2

I tried to parse the String from my input.txt file, but I get the NumberFormatException thrown every time. I've added all the code that I have so far. I have tried .trim as well. I am very new to text files in Java; therefore, I have no idea what is wrong here. However, any bit of help would be appreciated.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Lab5_Exceptions_Redo extends Exception {

    public static void main(String[] args) throws FileNotFoundException {

        String file = "inputt.txt";
        String file1 = "outputt.txt";
        String file2 = "errorr.txt";

        PrintWriter errorr = new PrintWriter(file2);

        PrintWriter inputt = new PrintWriter(file);
        inputt.println(15951);
        // int whatever = Integer.parseInt(file);
        // System.out.print(whatever);
        inputt.close();
        Scanner scan = new Scanner(file);
        String number = scan.nextLine();
        int num = Integer.parseInt(number.trim());
        System.out.printf("%d", num);

        PrintWriter outputt = new PrintWriter(file1);
        outputt.println(num);
        outputt.close();
        // inputt.close();

        scan.close();
        // System.out.printf("%d", num);

        try {

        } catch (InputMismatchException e) {
            errorr.println("There was an input mismatch error.");
        } catch (NoSuchElementException e) {
            errorr.println("There is no such element.");
        } catch (UnsupportedOperationException e) {
            errorr.println("An unsupported operation occured.");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        errorr.close();

    }
}
1
  • Did you try debugging the issue?. What do you see when you try to print num? Commented Mar 28, 2016 at 7:03

2 Answers 2

4

You are doing a mistake. In Scanner constructor you should pass File object not String object. Since you are passing string "inputt.txt" its trying to parse this string and as a result you are getting NFE. Try this

Scanner scan = new Scanner(new File(file));

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

Comments

0

You must be getting the whitespaces along with the input while doing nextLine(). Use trim() function to get rid of any whitespaces.

int num = Integer.parseInt(number.trim());

Alternatively, you can try using nextInt() instead of nextLine().

Here is the JAVA Doc of parseInt():

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

2 Comments

I tried the .trim, and unfortunately it does not work \: . I am very new to creating text files in Java, and that is why I am so confused as to why it will not parse the integer from the input text file.
This is not the actual root cause

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.