2013-03-12 47 views
0

這是一個簡單的程序,我寫了一個從txt文件中對整數進行計算的程序。經過幾個小時的調試之後,我希望有一組新的眼睛能夠發現我的一些問題。編輯:data.txt包含整數1-5。每行一個整數。NoSuchElementException Java

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Scanner; 

public class Calculator { 

public static int[] NUMBERS; //global value for the array 

public static void main(String[] args) throws FileNotFoundException { 
    File file = new File("data.txt"); 
    Scanner sc = new Scanner(file); 

    List x = new ArrayList(); 

    while (sc.hasNextInt()) { 
     x.add(sc.nextInt()); 
    } 

    NUMBERS = new int[x.size()]; 
    Iterator<Integer> iterator = x.iterator(); 
    for (int i = 0; i < NUMBERS.length; i++) { 
     NUMBERS[i] = iterator.next().intValue(); 
    } 

    sc.close(); 

    Scanner sc2 = new Scanner(file); 

    System.out.println("Welcome to Calculation Program!\n"); 
    startMenus(sc2); 

} 

private static void startMenus(Scanner sca) throws FileNotFoundException { 
    while (true) { 
     System.out.println("(Enter option # and press ENTER)\n"); 

     System.out.println("1. Display the average of the list"); 
     System.out.println("2. Display the number of occurences of a given element in the list"); 
     System.out.println("3. Display the prime numbers in a list"); 
     System.out.println("4. Display the information above in table form"); 
     System.out.println("5. Save the information onto a file in table form"); 
     System.out.println("6. Exit"); 

     int option = sca.nextInt(); 

     sca.nextLine(); 

     switch (option) { 
      case 1: 
       infoMenu1(sca); 
       break; 
      case 2: 
       infoMenu2(sca); 
       break; 
      case 3: 
       infoMenu3(sca); 
       break; 
      case 4: 
       infoMenu4(sca); 
       break; 
      case 5: 
       infoMenu5(sca); 
       break; 
      case 6: 
       System.exit(0); 
      default: 

       System.out.println("Unrecognized Option!\n"); 
     } 

    } 
} 

private static void infoMenu1(Scanner sc2) { 
    System.out.println("The average of the numbers in the file is: " + avg(NUMBERS)); 
} 

public static double avg(int[] numbers) { 
    int sum = 0; 

    for (int i = 0; i < numbers.length; i++) { 
     sum = (sum + numbers[i]); 
    } 

    return (sum/numbers.length); 
} 

public static int occr(int[] numbers, int x) { 
    int count = 0; 

    for (int n : numbers) { 
     if (n == x) { 
      count = count + 1; 
     } 
    } 

    return count; 
} 

public static boolean prime(int x) { 
    boolean answer = true; 

    if (x == 0 || x == 1) { 
     return false; 
    } 

    for (int i = 2; i <= x/2; i = i + 1) { 
     if (i != x) { 
      if (x % i == 0) { 
       answer = false; 
      } 
     } 
    } 

    return answer; 
} 

public static int[] primes(int[] numbers) { 
    int primesCount = 0; 

    for (int i : numbers) { 
     if (prime(i)) { 
      primesCount = (primesCount + 1); 
     } 
    } 

    if (primesCount == 0) { 
     return null; 
    } 

    int[] result = new int[primesCount]; 
    int index = 0; 

    for (int i : numbers) { 
     if (prime(i)) { 
      result[index] = i; 
      index = index + 1; 
     } 
    } 

    return result; 
} 

private static void infoMenu2(Scanner sc) { 
    sc = new Scanner(System.in); 
    System.out.println("Which integer would you like to check for number of occurences?"); 
    int choice = sc.nextInt(); 

    System.out.println("The number of occurence(s) of " + choice + " are/is " + occr(NUMBERS, choice)); 


} 

private static void infoMenu3(Scanner sc2) { 
    int[] myPrimes = primes(NUMBERS); 

    System.out.println("The prime number(s) in the file are/is: "); 

    for (int j = 0; j < myPrimes.length; ++j) { 
     System.out.println(myPrimes[j] + " "); 
    } 

} 

private static void infoMenu4(Scanner kb) throws FileNotFoundException { 
    File file = new File("data.txt"); 
    Scanner sc = new Scanner(file); 

    int counter = 0; 
    while (sc.hasNextInt()) { 
     counter = counter++; 
    } 

    int lenth = counter; 

    int[] column1 = new int[lenth]; 
    while (sc.hasNextInt()) { 
     column1 = new int[sc.nextInt()]; 
    } 

    int[] column2 = new int[occr(NUMBERS, sc.nextInt())]; 

    boolean column3 = prime(sc.nextInt()); 

    System.out.printf("Average: " + "%20.2f", column1); 
    System.out.println(); 
    System.out.printf("Occurences: " + "%14d", column2); 
    System.out.println(); 
    System.out.printf("Primes:" + "%19s : %s", column3); 
    System.out.println(); 
} 

private static void infoMenu5(Scanner kb) throws FileNotFoundException { 
    File file = new File("data.txt"); 
    Scanner sc = new Scanner(file); 
    PrintWriter pw = new PrintWriter(new File("results.txt")); 

    int counter = 0; 
    while (sc.hasNextInt()) { 
     counter = counter++; 
    } 

    int lenth = counter; 

    int[] column1 = new int[lenth]; 
    while (sc.hasNextInt()) { 
     column1 = new int[sc.nextInt()]; 
    } 

    int[] column2 = new int[occr(NUMBERS, sc.nextInt())]; 

    boolean column3 = prime(sc.nextInt()); 

    pw.printf("Number: " + "%-10d", column1); 
    pw.println(); 
    pw.printf("Occurences: " + "%12d", column2); 
    pw.println(); 
    pw.printf("Primes:" + "%24s : %s", column3); 
    pw.println(); 

    pw.close(); 
} 

}

+1

您可以發佈'data.txt'內容? – 2013-03-12 06:02:53

+1

你從哪裏得到這個問題?行號 – zdesam 2013-03-12 06:03:58

+0

由於您的主程序僅顯示main(String [] args)引發FileNotFoundException,因此您需要向我們顯示整個程序。因爲我們必須瞭解NoSuchElementException發生的位置。 – 2013-03-12 06:04:33

回答

1

您正在使用您的代碼(49行)錯誤的掃描儀。我想你正在嘗試獲取用戶輸入。但是,您正在使用Scanner從data.txt獲取輸入。

根據評論更新。實際上,你不需要startMenus中的data.txt中的Scanner。你需要的是用戶輸入。所以從System.in中將sc2更改爲Scanner。

原線31:

// Scanner sc2 = new Scanner(file); 
Scanner sc2 = new Scanner(System.in); 
+0

解決了一個主要問題。非常感謝 – lancer 2013-03-12 06:46:00

相關問題