【问题标题】:NumberFormatException when parsing negative integer解析负整数时出现 NumberFormatException
【发布时间】:2019-01-24 19:20:28
【问题描述】:

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

如果传递的参数是正整数,我的代码可以正常工作,但在传递负整数时会抛出 NumberFormatException。

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)

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

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;
        }

所以,我在下面的解决方案中使用字符串来解决这个问题。

注意:我们还没有进行拆分和正则表达式。

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;
    }
}

我在这里寻找两件事。

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

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

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

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

if(origNumber == reverse) // does not work

谢谢!

【问题讨论】:

  • edit您的问题包括您得到的数字格式异常的文本。包括堆栈跟踪并指出代码的哪一行引发了异常。
  • 如果您仔细阅读异常消息,您正在尝试解析一个空字符串。
  • 我理解异常,并在另一个线程中看到了该异常,但是我不确定如何解决该问题
  • 还有一个快速提示:当您已经将 123 转换为“123”时,您不再需要任何数学运算即可获得“321”。字符串由字符组成,并且由于您已经拥有一个字符串,因此只需将其中的字符反转即可。当您已经决定使用字符串时,数学很好,但根本不需要。

标签: java palindrome parseint


【解决方案1】:

好的,解决第一个和第二个问题的最简单方法就是使用 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").

希望有帮助!! ;)

【讨论】:

  • 谢谢!这绝对有帮助,尤其是比较字符串值的第三个问题!然而,即使我知道如何操作字符串和使用数学函数(我之前研究过 Java,虽然已经有一段时间了),我还是试图在没有它的情况下解决问题,因为我们还没有达到课程中的那部分.另外,如果我使用绝对值函数,我将无法在输出中正确显示原始负数
  • 如果回答您的问题,请不要忘记接受。
  • 我不确定是否接受这个作为解决方案。它会起作用,但是它并不能完全回答我原来的问题,因为它使用了我试图避免的绝对值函数(课程中还没有)。
  • 在使用数学函数之前将原始数字存储在不同的变量中,然后在最后使用该变量打印初始数字。
  • 向 NumberFormatException 添加一件事,当您使用字符串规则反转负数时,如果数字是“-354”,您将得到类似“453-”的内容,并且当您解析反向时肯定会得到例外。
猜你喜欢
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
相关资源
最近更新 更多