得到例外,我不知道为什么

时间:2013-04-10 01:01:38

标签: java runtime-error

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)

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

1 个答案:

答案 0 :(得分:1)

专注于:Exception in thread "main" java.util.NoSuchElementException: No line found

看看这段代码。

 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,请不要一次使用太多的下一个功能。你只会迷惑自己。