BadCoder
BadCoder

Reputation: 141

No Line Found - Java

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);

Upvotes: 0

Views: 189

Answers (3)

sparkitny
sparkitny

Reputation: 1533

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++;
    }
}

Upvotes: 0

user4398985
user4398985

Reputation:

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

Upvotes: 0

user201891
user201891

Reputation:

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.)

Upvotes: 2

Related Questions