gpt4 book ai didi

java - 出现异常,我不知道为什么

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:00 25 4
gpt4 key购买 nike

import java.util.Scanner;               //Import necessary classes 
import java.text.DecimalFormat;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;



public class Lab11
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("0.00");

final int LIMIT = 1000, MAX = 10, MIN = -10; //number of ints to be read into file
String fileName; //holds name of file


System.out.print("Enter the name of the file to hold the integers: ");
fileName = scan.next();
PrintWriter outFile = null; //creates PrintWriter to write to specified file

try
{
outFile = new PrintWriter(new File(fileName)); //TryCatch statement to catch any errors when file opens
}
catch (FileNotFoundException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}

for (int i = 0; i <= LIMIT; i++) { //for loop to write to file using random num between -10 and 10
outFile.print((int)(Math.random()*(MAX+1-MIN)) + MIN + " "); //makes sure numbers in file are separated by spaces
outFile.close(); //closes file for output
}


Scanner inFile = null; //creates scanner to read in file
try //TryCatch statement to catch errors
{
inFile = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}

int num, neg = 0, pos = 0, zero = 0; //variables to hold current number, positives, negatives and average
double avg = 0;

while (inFile.hasNext()) // Use a while loop to read in numbers from the file until the end
{
inFile.nextLine();
fileName = inFile.nextLine();
avg = 0;
num = 0;

while(inFile.hasNextInt())
{
num = inFile.nextInt();
avg += num; //adds current num to avg

if (num > 0)
pos++; //checks if positive and adds one to total pos
if (num < 0)
neg++; //checks is negative and adds one to total neg
else
zero++; //checks for zeros and adds one to total
}
avg = (avg/LIMIT); //calculates actual average
inFile.close(); //closes file for input
}

outFile = null; //Opens file for output again

try
{
outFile = new PrintWriter(new FileWriter(fileName, true)); //TryCatch statement to catch any errors when file opens
} //Allows file to be appended to instead of truncated
catch (IOException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}

System.out.println("\n\nNumber of negative numbers in the file: " + neg);
System.out.println("\nNumber of positive numbers in the file: " + pos);
System.out.println("\nNumber of zeroes in the file: " + zero);
System.out.println("\nAverage of the numbers in the file: " + fmt.format(avg));

outFile.print("Number of negative numbers in the file: " + neg);
outFile.print("Number of positive numbers in the file: " + pos);
outFile.print("Number of zeroes in the file: " + zero);
outFile.print("Average of the numbers in the file: " + fmt.format(avg));

outFile.close();
} // End of main
}

我正在介绍 Java,但我对它的态度很残酷。无论我练习或学习多少小时,我就是不明白。我已经完成了这么多,但现在我得到了这个。

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Lab11.main(Lab11.java:89)

我不知道我哪里错了。任何指导都会很棒。谢谢。

最佳答案

重点:线程“main”java.util.NoSuchElementException中出现异常:未找到行

看看这段代码。

 while (inFile.hasNext())        // Use a while loop to read in numbers from the file until the end
{
inFile.nextLine();
fileName = inFile.nextLine();

假设您的文件只有一行。然后,inFile.nextLine(); 已经读取了它。之后就没有更多的线路了。因此,删除 inFile.nextLine()

您的代码现在应该是:

 while (inFile.hasNext())        // Use a while loop to read in numbers from the file until the end
{
fileName = inFile.nextLine();

此外,正如 syb0rg 提到的,请不要一次使用太多的 next 函数。你只会让自己感到困惑。

关于java - 出现异常,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15915206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com