I have a String from which I would like to parse an integer and cannot find a way to this exception. I understand that it is intended to be displayed when the parseNUMBERTYPE function is applied to an unmanaged string, and that empty spaces or letters where the code expects a number can cause it. However, the line that I use as a test dummy, as far as I can tell, simply by the number 5. I saw several sentences in response to problems with other users of "NumberFormatException" protecting the use of the trim () function before parsing, and I tried it is without success.
I also tried replacing String, I want to parse a simple, non-configured value of "5". This is the same as the program, which appears to report that the corresponding variable stores the String value, but when parsing this variable with this post-eponymous exception, the unset value seems to work fine in its place.
Note that the String variable is read by the file scanner. I must assume that my problem has something to do with unknown, unwanted, "invisible" characters that are read in addition to the fifth, but I canβt determine why this is happening or how to stop it. File 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 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);
The following errors are presented to me:
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 help is appreciated.
In response to the suggestions so far, the code has been modified to look like this:
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 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 misinterpreted your guidance in some way. Be that as it may, the problem persists, with an identical error message.