解析负整数时出现NumberFormatException

时间:2019-01-24 19:20:29

标签: java palindrome parseint

我正在编写一个Java程序来确定数字是否是回文。

如果传递的参数是一个正整数,我的代码将正常工作,但是传递一个负整数时,将引发NumberFormatException。

"name":"test",
"version":1,
"user":[  
   {  
      "created":1548321909,
      "latest":false,
      "modified_date":<epoch time of created field converted to date format>
   },
   {  
      "created":1548321916,
      "latest":false,
      "modified_date":<epoch time of created field converted to date format>
   }

我从另一个stackoverflow线程中获得了以下解决方案,这似乎是讲师希望我们使用的解决方案,但是在while循环中,我假设由于负数始终小于0,因此它将退出循环而不计算回文数:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.stu.Main.isPalindrome(Main.java:28)
at com.stu.Main.main(Main.java:7)

因此,我在下面的解决方案中使用字符串来解决此问题。

注意:我们尚未拆分和正则表达式。

public static int reverse(int number)
        {  
            int result = 0;
            int remainder;
            while (number > 0)
            {
                remainder = number % 10;
                number = number / 10;
                result = result * 10 + remainder;
            }

            return result;
        }

我正在这里寻找两件事。

首先,在教师首选解决方案的情况下,如何解决while循环中带有负数的问题?

第二,如何解决我的解决方案中的NumberFormatException?

编辑:第三个问题。如果我从不解析回int,为什么我的解决方案返回false?

public class Main {

    public static void main(String[] args) {

        isPalindrome(-1221); // throws exception
        isPalindrome(707);   // works as expected - returns true
        isPalindrome(11212); // works as expected - returns false
        isPalindrome(123321);// works as expected - returns true
    }


    public static boolean isPalindrome(int number){

        if(number < 10 && number > -10) {
            return false;
        }

        String origNumber = String.valueOf(number);
        String reverse = "";

        while(number > 0) {
            reverse += String.valueOf(number % 10);
            number /= 10;
        }

        if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) {
            System.out.println("The original number was " + origNumber + "     and the reverse is " + reverse);
            System.out.println("Number is a palindrome!");
            return true;
        }
        else
            System.out.println("The original number was " + origNumber + " and the reverse is " + reverse);
            System.out.println("Sorry, the number is NOT a palindrome!");
            return false;
    }
}

if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) // works

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,解决您的第一个和第二个问题的最简单方法就是使用Math.abs(yourNumber)消除负号,仅此而已。

The java.lang.Math.abs() returns the absolute value of a given argument.

If the argument is not negative, the argument is returned.
If the argument is negative, the negation of the argument is returned.

这将解决您的第一个和第二个问题。

关于第三个问题,如果您不转换回整数,那么在比较需要使用equals()方法的字符串时会得到错误的答案,

== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they are logically "equal").

希望有帮助!! ;)