0

For the first time in mye relatively short programming life, I've run into a NoSuchElementException. I've done a bit of research, and none of the posts I've found trace back to a similar case. In many cases, the coder had simply not put in hasNextLine(), thus causing the Exception. Another example is when the coder tried to read two lines consequently, which also caused the error. A third tried got a solution with a check for an empty line at the end, and this I feel might lie closer to my problem. However, I do not feel this is the solution I'm looking for. Here is my code:

import java.util.HashMap; import java.util.Scanner; import java.io.*;

class DVDAdministrasjon
{
  private String eier;
  private String laaner;
  private boolean utlaant;
  private String tittel;

  private HashMap<String, DVD> dvdListe = new HashMap<String, DVD>();
  private HashMap<String, Person> personListe = new HashMap<String, Person>();

  public void lesDVDarkiv(String filnavn) throws Exception
  {
    Scanner fil = new Scanner(new File(filnavn));
    String curDVD = "";
    String inData; //lagrer data fra filen

    while (fil.hasNextLine())
    {
      inData = fil.nextLine();
      while (inData.equals("")) //Hopper over eventuelle linjeskift
      {
        inData = fil.nextLine();
      }
      if (inData.equals("-"))
      {
        inData = fil.nextLine();
        personListe.put(inData, new Person(inData));
      }
      else
      {
        inData = fil.nextLine();
        curDVD = inData;
        dvdListe.put(inData, new DVD(inData));
        if (curDVD.substring(0,1).equalsIgnoreCase("*"))
        {
          utlaant = true;
        }
      }
    }
  }
}

Here is the main class:

import java.util.Scanner; import java.io.File;

class Oblig7Test1
{
  public static void main(String[] args) throws Exception
  {
    Scanner in = new Scanner(System.in);
    DVDAdministrasjon dListe = new DVDAdministrasjon();
    dListe.lesDVDarkiv("dvdarkiv2.txt");
  }
}

Do keep in mind this program will be extended far beyond what is now visible. It is also for an assignment, but I am only interested in why I'm getting the error. The file consists of lines with more than one word in some lines, with "*" at the beginning of some lines and "-" as a divider between some lines.

The error looks like this:

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at DVDAdministrasjon.lesDVDarkiv(DVDAdministrasjon.java:29)
    at Oblig7Test1.main(Oblig7Test1.java:9)
yassadi
  • 498
  • 1
  • 9
  • 20

2 Answers2

1

In your code I have added some comments

while (fil.hasNextLine())  // if it enters here it has more lines
{
  inData = fil.nextLine();  // OK can read as hasNextLine returned true
  while (inData.equals("")) 
  {
    inData = fil.nextLine();  // NO guarantee that there is a `nextLine`

Without understanding you code fully I would suggest that if you need to read the next line then you should either contune to the top of your loop or test again using if (fil.hasNextLine())

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • I did not think of that, but it solved my problem. I understand the reasoning :-) I have definitely learnt something, and will bear this in mind while learning more! Thanks for your time! – Torstein Norum Bugge Oct 30 '16 at 11:00
0

In your second while loop do the following:

while (inData.equals("") && **fil.hasNextLine()**) //this second conditions ensure that it will enter while only if it has next line to read, else it passes to the next if condition
      {
        inData = fil.nextLine();
      }