-1

I am trying to read data from a text file and then store it to an array. I assume that there is one word per line. I am getting a NoSuchElementException here:

while (s.hasNextLine()) 
       {
           text = text + s.next() + " ";
       }

This is my code:

public class ReadNote 
{
   public static void main(String[]args) 
   { 


      String text = readString("CountryList.txt");
      System.out.println(text);

      String[] words = readArray("CountryList.txt");

      for (int i = 0; i < words.length; i++) 
      {
         System.out.println(words[i]);
      }
}


  public static String readString(String file) 
  {

       String text = "";

       try{
       Scanner s = new Scanner(new File(file));

       while (s.hasNextLine()) 
       {
           text = text + s.next() + " ";
       }

         } catch(FileNotFoundException e) 
           {
              System.out.println("file not found ");
           }
        return text;
   }


  public static String[] readArray(String file) 
  { 
      int ctr = 0;

       try {
       Scanner s1 = new Scanner(new File(file));

       while (s1.hasNextLine()) 
       {
            ctr = ctr+1;
            s1.next();
       }

       String[] words = new String[ctr];
       Scanner s2 = new Scanner(new File(file));

       for ( int i = 0; i < ctr; i++) 
       {
           words [i] = s2.next();
       }

        return words;

    } catch (FileNotFoundException e) { }
        return null;
 }
}

Here is the message.

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at ReadNote.readString(ReadNote.java:29)
    at ReadNote.main(ReadNote.java:13)
Idos
  • 15,053
  • 14
  • 60
  • 75
Lora Gonzales
  • 53
  • 1
  • 1
  • 6

3 Answers3

0

There are 2 issues with your code as far as I can tell:

  • You forgot to check hasNextLine() for your second Scanner s2.
    When using Scanner you need to check if there is a next line with hasNextLine(), and it will return null at EOF.
  • You probably want s.nextLine() instead of s.next() in your while loop since you are checking while (s1.hasNextLine()). In general, you have to match your .hasNext... to your .next....
Idos
  • 15,053
  • 14
  • 60
  • 75
0

For the specific exception you are getting in readString:

while (s.hasNextLine()) {
  text = text + s.next() + " ";
}

You need to either call s.hasNext() in the loop guard, or use s.nextLine() in the body.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

As described in this answer.

You have a single extra newline at the end of your file.

hasNextLine() checks to see if there is another linePattern in the buffer. hasNext() checks to see if there is a parseable token in the buffer, as separated by the scanner's delimiter.

You should modify your code to one of the following

while (s.hasNext()) {
    text = text + s.next() + " ";
}

while (s.hasNextLine()) {
    text = text + s.nextLine() + " ";
}
Community
  • 1
  • 1
DominicEU
  • 3,585
  • 2
  • 21
  • 32