0

I am inputting a txt file, here is a shortened version

10  
"Alexander McCall Smith" "No. 1 Ladies' Detective Agency"

I run this code:

Scanner in = new Scanner(new File(newFile + ".txt"));
int size = in.nextInt();
String inputLine = in.nextLine();

size ends up being 10, but inputLine ends up receiving nothing. I get the error

Exception in thread "main" java.util.NoSuchElementException: No line found.

I went to debugger and it says a string with position (-1, -1) is what java tries to insert into inputLine. I have no clue why, I know there is 50+ lines of text after 10. I ran in.next() and it worked just fine. Does anyone know why?

I run this code as well:

inputLine.trim();
int posn = inputLine.indexOf('\"');
int nextPosn = inputLine.indexOf('\"',posn + 1);
String author = inputLine.substring(posn, nextPosn);
BadCoder
  • 121
  • 1
  • 14

3 Answers3

2

Andrew Li has it right. Calling nextInt does not consume the line, so you're still on the first line, the one with "10" on it.

public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(Test.class.getResourceAsStream("input.txt"));
    int size = in.nextInt();
    String inputLine = in.nextLine();
    System.out.println(size); // prints "10"
    System.out.println(inputLine); // prints nothing

    inputLine.trim();
    int posn = inputLine.indexOf('\"');
    int nextPosn = inputLine.indexOf('\"', posn + 1);
    String author = inputLine.substring(posn, nextPosn); // Throws: "java.lang.StringIndexOutOfBoundsException: String index out of range: -1"
}

If you were to call nextLine twice in a row, you would get the "Alexander" line.

(I have no idea where you're getting a NoSuchElementException. It must be from somewhere else in your program.)

DavidS
  • 4,384
  • 2
  • 23
  • 51
  • Right so I created a "dummy" string and did .nextLine(), which afterwards fix my whole problem. Thanks a lot, I guess that was just a huge brainfart. – BadCoder Oct 22 '16 at 05:18
  • No worries, working with files is often frustrating. – DavidS Oct 22 '16 at 05:19
  • @TeenCoder No need for the dummy string. Calling `nextLine` will advance the Scanner, over the newline thus it should work. – Andrew Li Oct 22 '16 at 05:22
0

I wonder if you're not handling an end of line token appropriately. Often if you use Scanner#next###() (except for nextLine), and you reach an end of line token as when the user presses enter, if you don't handle the end of line token, it will prevent the Scanner object from working appropriately. To solve this, call Scanner#nextLine() when this token needs to be handled.

The below post has more details explaination. Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
JumpMan
  • 2,016
  • 3
  • 22
  • 37
0

If you have multiple entries in the file, you should loop over each line and process them individually. In your code searching for the "/" sign, if it is not found then the index will be -1 so you should handle these cases as well, here is an example:

public static void main(String[] args){
    Scanner in = null;
    int lines;
    int size;
    String inputLine;
    int posn, nextPosn;

    try {
        in = new Scanner(new File("testFile.txt"));
    }
    catch(IOException e){
        System.err.print("File read error: "+e.getMessage());
        System.exit(1);
    }

    lines = 0;
    // Iterate over the lines in the file
    while(in.hasNext()){
        inputLine = in.nextLine();
        System.out.println(inputLine);
        inputLine = inputLine.trim();
        if(lines == 0){
            // If you know that next line is an integer
            size = Integer.parseInt(inputLine);
        }
        posn = inputLine.indexOf('\"');
        nextPosn = inputLine.indexOf('\"',posn + 1);
        if(posn >=0 && nextPosn >=0) {
            String author = inputLine.substring(posn, nextPosn);
        }
        lines++;
    }
}
sparkplug
  • 1,451
  • 4
  • 18
  • 21