0
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at LineIO.main(LineIO.java:39)

There are no lines red out. I'll post code...

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
    public class LineIO {

    public static void main(String[] args)throws FileNotFoundException{
        Scanner console = new Scanner(System.in);
        System.out.print("Enter Input file name: ");
        String inputFileName = console.next();
        System.out.print("Output file: ");
        String outputFileName = console.next();
                File inputFile = new File(inputFileName);
                Scanner in = new Scanner(inputFile);
                PrintWriter out = new PrintWriter(outputFileName);
                int lineNumber = 1;
                int lineNumber2 = 10;
                int lineNumber3 = 20;
                int lineNumber4 = 30;
                int lineNumber5 = 40;



                System.out.println("Enter name 5 names:");
                String person1 = console.next();
                String person2 = console.next();
                String person3 = console.next();
                String person4 = console.next();
                String person5 = console.next();

                double sum = 0;
                int j = 0;
               while (j < 10)
                {
                  String line = in.nextLine();
                 int num = Integer.parseInt(line);
                  sum = sum+num;
                }
               double average = sum / 10;

            System.out.println(person1 + " average score is " + average);
            System.out.println(person2 + " average score is " + lineNumber2);
            System.out.println(person3 + " average score is " + lineNumber3);
            System.out.println(person4 + " average score is " + lineNumber4);
            System.out.println(person5 + " average score is " + lineNumber5);

                in.close();
                out.close();
    }
}

Why do I continue to get this error? I cannot understand the error message and do not understand why the code does not implement. I have my input.txt and output.txt files set up...

jake craigslist
  • 303
  • 3
  • 12

1 Answers1

2

A quick search came up with this post: java.util.NoSuchElementException: No line found

You must not have enough lines in the file.

In addition to making sure you have enough lines in your file, also add a check for a next line to avoid this error:

            while (j < 10)
            {
              if (in.hasNextLine()){
               String line = in.nextLine();
               int num = Integer.parseInt(line);
               sum += num; //minor change here
              }
              else{
                 System.out.println("Not enough lines!");
                 break;
              }
              j++; //increment j
            }
Community
  • 1
  • 1
Daniel Nugent
  • 40,780
  • 13
  • 103
  • 126
  • Thank you. Although it wasn't my only problem it helped show that I was missing a counter as well as off on my initial count (9 instead of 10). Yet I do not understand why the error disappeared when I input your if statement. Probably because I was missing a counter. Either way thank you for taking the time, if it was not for people like you these simple problems for beginners would make a newbie like me not want to code. Thank you. – jake craigslist Apr 09 '15 at 01:27
  • @jakecraigslist the check to `hasNextLine()` ensures that you will never get the exception in your question, simply because it prevents reading a line that doesn't exist. And yes, I can see that your code has other issues as well, but I was just addressing the question about the exception. – Daniel Nugent Apr 09 '15 at 01:38