每当我打开infile2.dat时,程序都会生成以前的错误。我不确定错误是在这个类中,还是在另一个类中。我已经玩过它,但似乎无法弄明白。如果有人能帮助我,我会非常感激。
我的代码中收到以下错误:
Exception in thread "main" java.util.NoSuchElementException
Just caught an illegal argument exception.
Just caught an illegal argument exception.
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at testfor6.TestFor6.main(TestFor6.java:58)
这是代码(很长时间,我提前道歉):
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
/** holds answer of whether user has more inputs or not */
char answer;
/** holds the results when calling inLetter() */
char letter;
/** holds the results when checking for types/strings in txt file */
char inType2;
/** holds double results when searching line by line the text file */
double inAmount2;
/** holds the results when calling inAmount() */
double amount;
/** initiates infile to null */
File infile2;
/** count system for how many valid lines were read and used */
int count = 0;
/** calls description method */
description();
/** initiates new Object */
NewClass newInput = new NewClass();
try{
/** defines new variable linked to .dat file */
infile2 = new File("GironEvent.dat");
Scanner fstream = new Scanner(infile2);
/** calls while loop. As long as there is a line, the loop continues */
while(fstream.hasNext())
{
try
{
/** inputs first string in line of file to variable inType */
inType2 = fstream.next().charAt(0);
/** inputs first int in line of file to variable inAmount */
inAmount2 = Double.parseDouble(fstream.next());
/** calls instance method with two parameters */
newInput.donations(inType2, inAmount2);
/** count ticket increases */
count+=1;
}
catch (IllegalArgumentException ex) {
/** prints out error if exception is caught*/
System.out.println("Just caught an illegal argument exception. ");
}
}
/** closes stream between program and fiel */
fstream.close();
}
catch (FileNotFoundException e){
/** Outputs error if file cannot be opened. */
System.out.println("\nGironEvent.dat could not be opened. ");
}
/** outputs line count, and totals per type */
System.out.println("\n" + count + " number of valid lines were read!\n");
System.out.println("Total Sales: " + newInput.getSale());
System.out.println("Donations: " + newInput.getDonated());
System.out.println("Expenses: " + newInput.getExpenses());
do{
/** loop asks user if there is more that needs to be added to the totals. N exits loop. */
System.out.print("\nAre there any more items to add that were not in the text file? (Type 'Y' or 'N'): ");
answer = keyboard.next().charAt(0);
if ((answer == 'y') || (answer == 'y'))
{
letter = inLetter();
amount = inAmount();
newInput.donations(letter, amount);
}
}while (((answer == 'y') || (answer == 'y')));
/** calls instance method to display totals in a fancy manner */
newInput.display();
}
/** inLetter - displays types to user. Asks user to input type. Converts input into uppercase letter.
*
* @return resultTwo - Uppercase form of result. Result is the type the user inputted.
*/
public static char inLetter(){
Scanner keyboard = new Scanner(System.in);
char result;
char resultTwo;
System.out.println("T = Tiket Sales");
System.out.println("D = Donations");
System.out.println("E = Expenses");
System.out.print("Please input an identifier ");
result = keyboard.next().charAt(0);
result = Character.toUpperCase(result);
return result;
}
/** inAmount - asks user for amount. Tests that amount isn't a negative number or a zero.
*
* @return result - the amount the user inputed. regardless if it was the first or tenth time.
*/
public static double inAmount(){
Scanner keyboard = new Scanner(System.in);
double result;
System.out.println("Please input an amount ");
result = keyboard.nextDouble();
if(result <= 0){
System.out.print("Please input a positive and non-zero amount ");
result = keyboard.nextDouble();
}
return result;
}
/** description - displays a description of what the program does
* void.
*/
public static void description(){
System.out.println("The program will ask you what amount is being spent on what.");
System.out.println(" ex: expenses, ticket sales, and profts.");
System.out.println("This program will help determine whether the event generated or lost money.");
}
-Manny