0

So we have a lab to do and it involves reading files and all that fun stuff.

This is what the txt file looks like:

Name0
22
1.2
Name1
22
2.71
Name2
19
3.51
Name3
18
3.91
Name4
20
1.6
Name5
19
1.03
Name6
18
3.78
Name7
19
3.19
Name8
18
2.37
Name9
21
1.01

I posted the code that is supposed to be trying to read this information. Thanks for your time!

I've tried changing some stuff around and googling the exception but with no luck

    public void readFile()
    {
        //ran intill exception caught
        try
        {
            //finds the student.txt file to read using scanners
            Scanner s = new Scanner("Students.txt");

            while(s.hasNextLine())
            {
                //sets a string name to the first string (First text in 
students is name)
                String name = s.next();
                //looks for a line with a int value and then sets it to 
age
                int age = s.nextInt(); 
                //scans the next line for a double and sets it to gpa

                double gpa = s.nextDouble();
                //creates a new student object and passes what the file 
read into parameters
                Student studentOne = new Student(name , age, gpa);
                //adds new student to array list
                students.add(studentOne);
            }
            s.close();
        }
        // if an exception is caught it will print
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

I believe it was supposed to read the information and store it in the respected category since we know it goes in this order based on the text file but when I run the method I get java.util.NoSuchElementException

Kninja99
  • 3
  • 2
  • in the text file, each piece of data is on a new line – Kninja99 May 25 '19 at 17:04
  • Can you share the stack trace of the exception you are getting? – Muhammad Abdurrahman May 25 '19 at 17:09
  • I think because you call `s.nextDouble()` and then call `s.next()` your lines are getting "off" and you're not reading correctly. Try adding a `s.nextLine()` after the `s.nextDouble()`. – markspace May 25 '19 at 17:11
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – markspace May 25 '19 at 17:12
  • @MuhammadAbdurrahman how would i get the stack trace im new to this – Kninja99 May 25 '19 at 17:34
  • @markspace how would i fix a duplicate scanner – Kninja99 May 25 '19 at 17:36
  • Kninja99, the example of the input file you presented seems inconsistent, as one of the names are missing. Do the names come after the gpa or before the age? If it's the former, then a name is missing on the last line. Otherwise if it's the latter, a name is missing on the first line. Please clarify – Muhammad Abdurrahman May 25 '19 at 17:45
  • @MuhammadAbdurrahman The name is the first given then it indents to the next line and gives age and then indents to next line and goes to gpa – Kninja99 May 25 '19 at 17:53
  • @Kninja99, can you update the example of the file in your question so we can see exactly how it should look? – Muhammad Abdurrahman May 25 '19 at 17:54
  • Thanks everyone for their time I think I found my solution. – Kninja99 May 25 '19 at 18:04
  • the scanner should of been declared, Scanner s = new Scanner(new File("Students.txt")) – Kninja99 May 25 '19 at 18:04

1 Answers1

0

You are receiving NoSuchElementException because calling the nextInt() and nextDouble() methods on the Scanner object do not read the new line characters (the character created when hitting return) - see this answer.

To fix this you can do something like the following:

public void readFile() throws IOException {
    try (Scanner s = new Scanner(new FileReader(new ClassPathResource("Students.txt").getFile()))) {
        while (s.hasNextLine()) {
            String name = s.nextLine();
            int age = Integer.parseInt(s.nextLine());
            double gpa = Double.parseDouble(s.nextLine());
            Student studentOne = new Student(name, age, gpa);
            students.add(studentOne);
        }
    }
}

Note - the code above assumes the Students.txt file is in your classpath.