Exception in thread "main" java.util.Nosuchelementexception

Asked

Viewed 2,199 times

2

I have the following java method that reads a text file and creates new points (coordinates on a graph), but I am encountering the error title in reading the file. By following my stackTrace it points out that the error is when I try to read the first nextDouble(). Another curious thing is that sometimes it works normally, then I try to increase the number of dots in my text file and it starts to freak out.

public void readDatabase(String s) throws FileNotFoundException{    
    try {               
        BufferedReader br = new BufferedReader(new FileReader(s));
        String line = br.readLine();
        Scanner trainFile = null;
        while (line != null) {      
            line.trim();
            trainFile = new Scanner(line);
            double x = trainFile.nextDouble();
            double y = trainFile.nextDouble();
            int type = trainFile.nextInt();
            this.database.add(new Ponto(x,y,type));
            line = br.readLine();
        }   
        trainFile.close();
        br.close();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

Does anyone have any idea what the problem is?

2 answers

2

Are there empty lines in your file? (the last maybe) A documentation of getDouble says:

Nosuchelementexception - if the input is exhausted

I notice you do trim on the line and proceed to the reading via Scanner, without checking if that line is empty. I suggest doing this and, if so, continuing with the loop:

  while (line != null) {        
      line = line.trim(); // Nota: Strings são imutáveis - é preciso reatribuir o valor após o trim
      if ( line.length() == 0 )
          continue;
      trainFile = new Scanner(line);
      ...
  • The problem with empty lines at the end of the file was something that crossed my mind, but I made sure to delete the last line to make sure that the cursor is located in the last character of the line above. (This assures me that the last line is not empty, correct?)

  • 1

    I think so. If the problem remains, how about posting an input example in Pastebin or similar (one that gives the correct output, and one that gives error)? Because unless someone sees a problem in the code that I’m not seeing, it seems okay. So I still think the error should be in the data file.

  • An input type was a plain text file with two double values and one int per line separated by whitespace. The interesting thing is that when using . as separator I got error, but when using , my code worked.

0


To solve the problem I made the following changed the reading conditions of the next line in my code, so I did not come across invalid lines.

public void readDatabase(String s) throws FileNotFoundException{    
    try {               
        final BufferedReader br = new BufferedReader(new FileReader(s));
        final Scanner trainFile = new Scanner(br);
        while (trainFile.hasNextDouble()) {      
            double x = trainFile.nextDouble();
            double y = trainFile.nextDouble();
            int type = trainFile.nextInt();
            this.database.add(new Ponto(x,y,type));
        }   
        trainFile.close();
        br.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.