我对Java和编程领域还是陌生的,并且一直在从事学校作业,我似乎无法弄清楚。我不断收到如下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at assignments.DonationTextFiles.menu(DonationTextFiles.java:30)
at assignments.DonationTextFiles.main(DonationTextFiles.java:206)
我正在尝试创建一个菜单,用户在其中输入int值以选择选项,并在方法运行后重复菜单。菜单方法现在看起来像这样:
package assignments;
import java.util.Scanner ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class DonationTextFiles
{
public static void menu(){
Scanner keyboard = new Scanner (System.in);
int input = 0;
while(true) {
System.out.println("Choose and option for the menu");
System.out.println( "1. Create a file \n"+
"2. Append to the file \n" +
"3. Print the file \n" +
"4. Locate the largest donation\n" +
"0. Exit Program \n" +
"Make a selection");
input = keyboard.nextInt();
switch(input){
case 0:
System.out.println("End Processing");
System.exit(0);
break;
case 1:
createFile();
break;
case 2:
appendFile();
break;
case 3:
printFile();
break;
case 4:
locateHighest();
break;
default:
System.out.println("Please enter a number between 0 and 4");
break;
}
}
}
public static void locateHighest()
{
System.out.println("I will read from the text file Donations.txt and display it to the console");
Scanner inputStream = null;
try
{
inputStream = new Scanner (new FileInputStream("Donations.txt"));
}
catch (FileNotFoundException e)
{
System.out.print("Donations.txt was not found");
System.out.println(" or could not be opened");
System.exit(0);
}
String name = inputStream.nextLine ();
double amount = inputStream.nextDouble();
double temp = 0;
while(inputStream.hasNext()) {
if(temp > inputStream.nextDouble()) {
}else {
temp = inputStream.nextDouble();
}
name = inputStream.nextLine ( );
amount = inputStream.nextDouble();
if(!inputStream.hasNext()){
if(temp < inputStream.nextDouble()) {
temp = inputStream.nextDouble();
}
}
}
inputStream.close();
System.out.println(temp);
}
public static void createFile()
{
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter (new FileOutputStream("Donations.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file Donations.txt");
System.exit(0);
}
System.out.println("Creating a file");
outputStream.close();
System.out.println("End of processing");
}
public static void printFile()
{
System.out.println("I will read from the text file Donations.txt and display it to the console");
Scanner inputStream = null;
try
{
inputStream = new Scanner (new FileInputStream("Donations.txt"));
}
catch (FileNotFoundException e)
{
System.out.print("Donations.txt was not found");
System.out.println(" or could not be opened");
System.exit(0);
}
String line = inputStream.nextLine ( );
double amount = inputStream.nextDouble();
System.out.println("Name" + "\t\t" + "Donation");
while(inputStream.hasNext()) {
System.out.println(line + "\t\t"+ amount);
line = inputStream.nextLine ( );
amount = inputStream.nextDouble();
if(!inputStream.hasNext()){
System.out.println(line + "\t\t"+ amount);
}
}
inputStream.close();
System.out.println("End of processing");
}
public static void appendFile()
{
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter (new
FileOutputStream ("Donations.txt", true));
}
catch (FileNotFoundException e)
{
System.out.println ("Error opening the file Donations.txt");
System.exit(0);
}
Scanner keyboard = new Scanner(System.in);
String name = ""; double amount = 0.0;
System.out.println("Enter the donor name and amount of donation");
name = keyboard.nextLine();
amount = keyboard.nextDouble();
outputStream.println(name);
outputStream.println(amount);
outputStream.close();
keyboard.close();
System.out.println("End of Append Processing");
}
public static void main(String[ ] args)
{
menu();
}
}
}
我会得到这样的输出,所以
当我尝试使用appendFile()将项目追加到文本文件时,会出现问题。它会追加,但菜单无法再次运行。 createFile()似乎可以正常运行两次。我认为Scanner以及我的使用方式应该有问题。有人可以帮我吗?
在系统退出之前,请不要关闭扫描仪。也许可以在“ System.exit(0);”之前的“ 0”案例语句中执行此操作。