Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What exception does numberformatexception refer to?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what anomalies numberformatexception refers to. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Numberformatexception represents a numeric formatting exception. You need to check that the string is interspersed with string or other types. Note that the content in the text must be a numeric string.

Today, there is a digital conversion exception, after dealing with it, a few scenarios are summarized.

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

The java.lang.NumberFormatException number format is abnormal. This exception is thrown when an attempt is made to convert a String to a specified numeric type and the string does not meet the format required by the numeric type.

Invalid int: "0" prompts an error while converting "0" to a numeric type.

Which line of which method is wrong? take a look at the error stack below. At com.example.myclock.TimerView$5.onTextChanged (TimerView.java:95) in the onTextChanged method of the com.example.myclock.TimerView class, line 95 of imerView.java is wrong.

. -cause analysis-

"0" has a space after 0, which should be removed when the string is converted to a number.

-solution-

For example: int vale=Integer.parseInt (s.toString (). Trim ()); / / ToString () is a method that converts to a string. Trim () is a method that removes spaces on both sides of a string.

Other cases where NumberFormatException is thrown:

In case 1, it is beyond the range of conversion value types:

A NumberFormatException exception is thrown when converting characters with Integer.parseInt (). It's okay to shorten the characters.

String line3 [1] = "8613719716"

Int int1=java.lang.Integer.parseInt (line3 [1])

This is a small segment of the program, but an exception is always thrown during the run

Exception in thread "main" java.lang.NumberFormatException: For input string: "8613719716"

-cause analysis-

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

-solution-

8613719716 cannot be expressed directly in int, only in long. If it is larger, you have to use BigInteger. Long.parseLong (String).

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

In case 2, the conversion value type does not take into account the situation where the value is empty:

Whether this sequence is correct in Android, I intend to convert the values in the resulting edittext to integers.

StartTime_hour_int=Integer.parseInt (startTime_hour_edittext.getEditableText () .toString ())

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

. -cause analysis-

If textbox startTime_hour_edittext is empty, Integer.parseInt will try to convert "" to integer. This is why NumberFormatException appears. So you need to determine whether the textbox startTime_hour_edittext is empty before converting to the int type.

-solution-

Before using startTime_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 the difference of the base system:

The main purpose of the question is to do a binary conversion. And the range is limited to 30 digits (1073741823) or (01111111111111111111111111111111111111111). The problem occurs when trying to convert 11111111111111111111111111111111111111111111111111111111, NumberFormatException.

This code checks that the input is converted to an int value if it is binary.

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

This is the code that checks 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;}

An exception occurred:

Java.lang.NumberFormatException: For input string: "11111111111111111111111111111111"

-cause analysis-

Because Integer.parseInt (String) defaults to decimal.

So you need to use Integer.parseInt (String, int) and specify the n of the n-ary numbers to convert. For example, the binary system is 2.

-solution-

Int value = Integer.parseInt (input, 2); this is the end of the article on "what does numberformatexception mean?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report