Home>Article> What is numberformatexception?

What is numberformatexception?

David Beckham
David Beckham Original
2021-01-14 18:25:46 59087browse

numberformatexception indicates a number formatting exception. You need to check whether the string is mixed with string or other types. It is important to note that the content in the text must be a string in the form of a number.

What is numberformatexception?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

A digital conversion exception occurred today. After handling it, I briefly summarized several scenarios.




#E/AdroidRutime: Fatal exception: java.lang.NumberFormatException: Invalid int: "0 "

##java.lang .NumberFormatExceptionNumber format exception. This exception is thrown when trying to convert aStringto a specified numeric type, but the string does not meet the format required by the numeric type.

Invalid int: "0 "TipsConvert "0 " An error occurred when converting it into a numeric type.

Specifically, the error occurred in which method of which class and which line. Please see the error stack below,at com. example.myclock.TimerView$5.onTextChanged(TimerView.java:95)In theonTextChangedmethod of thecom.example.myclock.TimerViewclass, An error occurred on line 95 ofimerView.java##.

---- --Cause Analysis--------------------

"0 "There is a space after 0, and the space should be removed when the string is converted into a number.

------solution--------------------

For example:intvale=Integer.parseInt(s.toString() .trim());//ToString() is converted into a string The method Trim() is a method to remove spaces on both sides of a string.

Other throwingNumberFormatException situations:

Case 1 , beyond the conversion range of numerical types:

Thrown when using Integer.parseInt() to convert charactersNumberFormatExceptionException, change the characters to a shorter length and it will be fine
##String line3[1]= "8613719716 ";
int int1=java.lang.Integer.parseInt(line3[1]);

The above is a short section of the program, but when running Exceptions are always thrown during the process
Exception in thread "main " java.lang.NumberFormatException: For input string: "8613719716 "

------Cause Analysis-------------- ------

The storage range of int type is -2,147,483,648 --2,147,483,647.Use System.out.println(Integer.MAX_VALUE); the output is 2147483647.And your String line3[1]= "8613719716 "; exceeds this maximum value.

------solution--------------------

8613719716 It cannot be directly represented by int, you can only use long,If it is larger, you have to use BigInteger.Long.parseLong(String).

Reference: http://www.myexception.cn/j2se/NumberFormatException.html

Case 2, The conversion value type does not consider the situation of the value being empty:

Is this sequence correct in Android? I plan to put the obtainededittext Convert the value to an integer.

##startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());

The following error occurred in logcat.05-12 10:26:35.536: ERROR/AndroidRuntime(293): java.lang.NumberFormatException: unable to parse ' ' as integer

.------Cause Analysis--------------------

If textbox startTime_hour_edittext is empty, Integer.parseInt will try to convert "" into integer. This is why NumberFormatException occurs. Therefore, you need to determine whethertextbox startTime_hour_edittext is empty before converting to int type.

------solution--------------------

Before usingstartTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());

Judgment conditions:

if(!startTime_hour_edittext.getText().toString().equalsIgnoreCase("")) { startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString()); }

Case 3, due to different base systems:

The question mainly involves a base conversion. And The range is limited to 30-digit numbers (1073741823) or (0111111111111111111111111111111). The problem occurs when trying to convert 1111111111111111111111111111111. NumberFormatException.

This code checks if the input is binary and converts it to int. Type value

if (checkNumber(input)) { try { number = Integer.parseInt(input); } catch (NumberFormatException ex) { log(ex.getMessage()); } } else { toDecimal(); }

This is the code for checking the Boolean return value method of String.

private static boolean checkNumber(String input) { for (char c : input.toCharArray()) { if (!Character.isDigit(c)) { return false; } } return true;}

Exception occurred:

java.lang.NumberFormatException: For input string: "111111111111111111111111111111"

------Cause Analysis------ ---------------

因为Integer.parseInt(String)默认是十进制.

所以需要使用Integer.parseInt(String, int)并且指定要转换的n进制的数字的n。比如二进制是2.

------解决方案--------------------

int value = Integer.parseInt(input, 2);

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What is numberformatexception?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn