0

I'm doing a programming project and keep getting the error shown below.

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at ArrayPhoneDirectory.loadData(ArrayPhoneDirectory.java:42)
    at ArrayPhoneDirectoryTester.main(ArrayPhoneDirectoryTester.java:18)

I thought this would be because the scanner read.nextLine() is going past the end of the text file. But I've used a while loop with hasNextLine so I'm not sure why this is happening. Anyone know where I'm going wrong?

 public void loadData (String sourceName){
Scanner read = new Scanner(sourceName);

while (read.hasNextLine()) {
    String name = read.nextLine();
    String telno = read.nextLine(); //ArrayPhoneDirectory Line 42
    add(name, telno);
  }
 }

Associated text file

John
123
Bill
23
Hello
23455
Frank
12345
Dkddd
31231
Sam
  • 178
  • 1
  • 1
  • 10
  • how is your data? can you please show? Is it `name` in one line and `telno` in another and so on? or what? – tod Apr 24 '14 at 14:30
  • @tod My data looks exactly as you guess. Below is the short version I'm testing with. John 123 Bill 23 Hello 23455 Frank 12345 Dkddd 31231 – Sam Apr 24 '14 at 14:32
  • @Sam If you have some additional informations which could be relevant to question then you should put them in your question, not just post it as comment. So try to use [[edit]] option under your question and include your additional informations there. – Pshemo Apr 24 '14 at 14:34
  • @Sam can you add it into the question, it is not clear here in the comment.. – tod Apr 24 '14 at 14:35
  • Have now added to question, thanks :) – Sam Apr 24 '14 at 14:39
  • Data you posted do not cause problem you described. Are you sure that you don't have odd number of lines you are scanning? – Pshemo Apr 24 '14 at 14:41
  • @Pshemo Yes I have posted the entirety of the data file – Sam Apr 24 '14 at 14:42
  • If you take a look at [this example](http://ideone.com/bZmABg) you will see no problems with your code and your data. Can we see how are you using your method? – Pshemo Apr 24 '14 at 14:44

5 Answers5

6

You are reading two lines while only checking for the existence of one

Here is the second read

String telno = read.nextLine(); //ArrayPhoneDirectory Line 42
geoand
  • 49,266
  • 16
  • 140
  • 156
  • Thanks, is there an easy way to fix this? – Sam Apr 24 '14 at 14:29
  • No problem. One solution would be to use `read.hasNextLine()` again before reading the line. Another would be to actually have both pieces of data on the same line and parsing that line into the name and telno – geoand Apr 24 '14 at 14:32
1

hasNextLine checks for only one line. You are trying to read two lines.

String name = read.nextLine();
String telno = read.nextLine();

which in case of odd number of lines can throw NoSuchElementException for second line you want to read.

Pshemo
  • 113,402
  • 22
  • 170
  • 242
0
hasNextLine()

will check only for one new line. you cannot read two lines after checking for just one.

If you have to continuously read records then you could do

public void loadData (String sourceName){
    Scanner read = new Scanner(sourceName);
    int i = 1;

    while (read.hasNextLine()) {
        if(i%2 != 0)
            String name = read.nextLine();
        else
            String telno = read.nextLine(); //ArrayPhoneDirectory Line 42
        add(name, telno);
        i++;
    }
}
Aniket Thakur
  • 58,991
  • 35
  • 252
  • 267
  • I'm yet to get test it fully but this seems to have solved the errors I was getting, thanks :) – Sam Apr 24 '14 at 14:41
0

Once you call nextLine the pointer gets incremented. Since, you have already called it before in this line:

String name = read.nextLine();

So, the next time you try to read it over here:

String telno = read.nextLine();

you get no such element exception. You should use this instead:

String telno = name 
Ankit Bansal
  • 4,521
  • 20
  • 39
-1

What you're doing is reading too many lines when checking for only one. It goes like this:

Problem

If the array on "lines" looks like this:

["This is line 1"]

Then read.hasNextLine() will return true. Then you enter your while loop. You run the first line which is:

String name = read.nextLine();

You retrieved one element from the above array, now it looks like this:

[]

Then you continue through your while loop:

String telno = read.nextLine();

Then the nextLine() method looks for a element in the array to give you, doesn't find any, and throws an exception.

Andrew Gies
  • 719
  • 8
  • 20