0

I am reading a set of integer values in from a .txt file in Java using Scanner and FileReader. There is one value per line in the input file. All of the values are read into the StringBuilder variable, but for some reason the compiler throws an exception right at the end of reading the input in the while loop and I can't quite figure out why.

    Scanner inFile;
    String value = "";
    String arrayString = "";
    StringBuilder sb = new StringBuilder();
    try {
        inFile = new Scanner(new FileReader("data.txt"));
        value = inFile.next();
        while (inFile.hasNextLine()) {
            sb.append(value);
            value = inFile.next();
        }

        arrayString = sb.toString(); // Not executing
        System.out.println(arrayString); // Not executing
        inFile.close();

    } catch (Exception e) {
        System.out.println("Error: File not found.");
    } finally {
        // inFile.close();
    }
    System.out.println(arrayString); // Not executing
6
  • 2
    what type of exception? Commented Aug 16, 2014 at 10:28
  • paste exception trace Commented Aug 16, 2014 at 10:30
  • 3
    The compiler never throws exceptions. Is it a compiler error that you get while compiling, or do you get an exception while running the code? Commented Aug 16, 2014 at 10:30
  • Instead of printing "Error: File not found." please use e.getMessage(). Commented Aug 16, 2014 at 10:32
  • Use either scanner.hasNextLine() and scanner.nextLine() OR scanner.hasNext() and scanner.next(), but don't mix them. Empty lines can cause issues like the one you're having. Commented Aug 16, 2014 at 10:44

3 Answers 3

1

the compiler throws an exception right at the end of reading the input in the while loop

A compiler does not "throw exceptions". Instead it gives errors/warnings related to issues in your code itself.

Having said this, "at the end of reading the input" implies your file may contain new line characters after the set of integer values. If this is the case then your Scanner will throw a java.util.NoSuchElementException when calling value = inFile.next();.

You can try the following way of reading instead:

while (inFile.hasNextLine()) {
    value = inFile.nextLine();
    sb.append(value);
}

Also try to print the exception message in your catch block. A java.util.NoSuchElementException does not mean Error: File not found.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, you probably get a FileNotFoundException on inFile = new Scanner(new FileReader("data.txt"));

You could also get several exceptions when you are reading a file using a scanner. You are catching an Exception so everything will be caught.

Now the lines :

arrayString = sb.toString(); // Not executing
System.out.println(arrayString); // Not executing
inFile.close(); // --> Is also not executed. Resource leak here. put it in finally.

are outside the try{} block, so, they will not get executed. catch{}gets executed because you have an exception andfinally{}` gets executed always.

So, System.out.println(arrayString); is outside of the try{} , catch{}, finally{} scope, so it will not get executed.

Comments

0

I created file with integer from 1 to 6 , same code is working fine. If there is no blank space on last line. But If I am keeping last line with blank or space , then its going into Exception. just check, is your last line of data.txt file having blank space?

1 Comment

Sir@thecbuilder, I don't have 50 reputation to make a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.