I have a String from which I would like to parse an integer and cannot find a way past this runtime exception. I understand that it is meant to display at times when a parseNUMBERTYPE function is applied to an inappropriately defined String, and that blank spaces or letters where the code expects numbers to be can trigger it. However, the String I am using as a test dummy is as far as I can tell simply the numeral 5. I have seen several suggestions in response to other users' NumberFormatException problems advocating the application of a trim() function before parsing, and I have tried this with no success.
I have also tried replacing the String I wish to parse with the simple, unstored value "5". This is the same as what the program seems to report as the relevant variable's stored String value, but while parsing that variable fails with this post's eponymous exception, the unstored value appears to run perfectly well in its place.
Note that the String variable is read by a File Scanner. I must suppose my problem has something to do with unknown, unwanted, 'invisible' characters that are being read in addition to the number five, but I cannot determine why this is happening or how to stop it. The file is formatted as .txt
Here is my code:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename = scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign to the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added later to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println(line1);
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem may in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1);
I am presented with these errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Driver.main(Driver.java:26)
Your assistance is appreciated.
In response to the suggestions given so far, the code has been modified to appear as follows:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename=scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added after to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println("["+line1+"]");
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem is in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1.trim());
Please correct me if I have misinterpreted your guidance in some way. As it stands, the problem persists, with an identical error report.