NumberFormatException, given input String containing a small integer

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.

+6
source share
1 answer

Looking at the message in the exception, it shows that there are no additional visible characters or spaces in the string, since 5 is surrounded by quotation marks (and a quick check of the Java source code shows that the message printed here does not seem to be changed to remove the spaces before how to surround the string with quotes).

This means that your line has hidden non-printable characters, or 5 itself is not 5 at all, but instead is a unicode character from another language that resembles 5.

A simple debugging case to sort this out will print the length of your string, as this will quickly figure out if there are any extra hidden characters in it.

 System.out.println("String: [" + line1 + "] size: " + line1.size()); 

After that, the regular expression can be used to get the first consecutive set of digits, if this is the desired behavior:

  Pattern pattern = Pattern.compile("(\\d+)"); Matcher matcher = pattern.matcher(line1); if (matcher.find()) { String digits = matcher.group(); int num = Integer.parseInt(digits); } 

Alternatively, if it is possible or desirable to remove characters between numbers, you can use replaceAll :

  String digits = line1.replaceAll("[^0-9]",""); 

For the string "5 6", the first method will give you 5, and the second will give you 56.

+1
source

Source: https://habr.com/ru/post/954805/


All Articles