java – 什么是NumberFormatException以及如何解决?

weixin_38078044 2019-09-12 12:57:02
参见英文答案 > What is a stack trace, and how can I use it to debug my application errors?                                    7个 Error Message: Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at set07102.Cards.main(Cards.java:68) C:\Users\qasim\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) 我的循环: while (response != 'q' && index < 52) { System.out.println(cards[index]); int first_value = Integer.parseInt(cards[index]); int value = 0; //Add a Scanner Scanner scanner = new Scanner(System.in); System.out.println("Will the next card be higher or lower?, press q if you want to quit"); String guess = scanner.nextLine(); if(cards[index].startsWith("Ace")) { value = 1; } if(cards[index].startsWith("2")) { value = 2; } if(cards[index].startsWith("3")) { value = 3; } //checking 4-10 if(cards[index].startsWith("Queen")){ value = 11; } if(cards[index].startsWith("King")){ value = 12; } if(guess.startsWith("h")){ if(value > first_value){ System.out.println("You answer was right, weldone!"); } else { System.out.println("You answer was wrong, try again!"); } } else if(guess.startsWith("l")){ if(value < first_value) { System.out.println("You answer as right, try again!"); } else { System.out.println("You answer was wrong, try again!"); } } else { System.out.println("Your was not valid, try again!"); } scanner.close(); index++; }//end of while loop
...全文
639 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38092709 2019-09-12
  • 打赏
  • 举报
回复
Error Message: Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at set07102.Cards.main(Cards.java:68) C:\Users\qasim\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 手段: There was an error. We try to give you as much information as possible It was an Exception in main thread. It's called NumberFormatException and has occurred for input "Ace of Clubs". at line 65th of NumberFormatException.java which is a constructor, which was invoked from Integer.parseInt() which is in file Integer.java in line 580, which was invoked from Integer.parseInt() which is in file Integer.java in line 615, which was invoked from method main in file Cards.java in line 68. It has resulted in exit code 1 换句话说,您尝试将“Ace of Clubs”解析为一个int,而Java无法使用Integer.parseInt方法. Java提供了漂亮的堆栈跟踪,它可以准确地告诉您问题所在.您正在寻找的工具是调试器,使用断点将允许您在所选时刻检查应用程序的状态. 如果要使用解析,解决方案可能是以下逻辑: if (cards[index].startsWith("Ace")) value = 1; else if (cards[index].startsWith("King")) value = 12; else if (cards[index].startsWith("Queen")) value = 11; ... else { try { Integer.parseInt(string.substring(0, cards[index].indexOf(" "))); } catch (NumberFormatException e){ //something went wrong } } Java中的异常是什么? An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. –Documentation Integer#parseInt中的构造函数和用法 static NumberFormatException forInputString(String s) { return new NumberFormatException("For input string: \"" + s + "\""); } public NumberFormatException (String s) { super (s); } 它们对于理解如何读取堆栈跟踪非常重要.看看如何从Integer#parseInt抛出NumberFormatException: if (s == null) { throw new NumberFormatException("null"); } 或者以后,如果输入String的格式不可解析: throw NumberFormatException.forInputString(s); 什么是NumberFormatException? Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. –Documentation NumberFormatException扩展了IllegalArgumentException.它告诉我们它是更专业的IllegalArgumentException.实际上,它用于突出显示虽然参数类型是正确的(字符串)但字符串的内容不是数字(a,b,c,d,e,f在HEX中被认为是数字,并且在需要时是合法的). 我如何解决它?好吧,不要解决它抛出的事实.抛出它是件好事.有些事情你需要考虑: >我可以阅读堆栈跟踪吗?>导致异常的String是否为null?>它看起来像一个数字?>是’我的字符串’还是用户的输入?>继续 广告. 1. 消息的第一行是Exception发生的信息和导致问题的输入String.字符串始终如下:并引用(“some text”).然后你开始对从最后读取堆栈跟踪感兴趣,因为前几行通常是NumberFormatException的构造函数,解析方法等.然后最后,你的方法中有一个bug.将在其调用的文件和方法中指出.即使是一条线也会被附加.你会看到的.上面是如何读取堆栈跟踪的示例. 广告. 2. 当你看到,而不是“For input string:”和输入时,有一个null(不是“null”),这意味着你试图将空引用传递给一个数字.如果您真的想要处理为0或任何其他数字,您可能会对我在StackOverflow上的另一篇文章感兴趣.它可用here. StackOverflow线程What is a NullPointerException and how can I fix it?详细描述了解决意外空值的描述. 广告. 3. 如果您引用的引用后面的字符串看起来像一个数字,则可能是您的系统无法解码的字符或看不见的空格.显然“6”无法解析,“123”也无法解析.这是因为空间.但它可能会发生,字符串将看起来像“6”,但实际上它的长度将大于您可以看到的位数. 在这种情况下,我建议使用调试器或至少使用System.out.println并打印您尝试解析的String的长度.如果它显示的位数超过了位数,请尝试将stringToParse.trim()传递给解析方法.如果它不起作用,请复制整个字符串:并使用在线解码器对其进行解码.它会给你所有字符的代码. 我最近在StackOverflow上发现了一个案例,您可能会看到输入看起来像一个数字,例如“1.86”并且它只包含这4个字符,但错误仍然存​​在.请记住,只能用#Integer#parseInt#解析整数.对于解析十进制数,应该使用Double#parseDouble. 另一种情况是,当数字有多位数时.它可能是,它太大或太小,无法适应int或long.您可能想尝试新的BigDecimal(< str>). 广告. 4. 最后,我们来到了我们同意的地方,当用户输入“abc”作为数字字符串时,我们无法避免这种情况.为什么?因为他可以.幸运的是,这是因为他是测试人员或者只是一个极客.在一个糟糕的情况下,它是攻击者. 我现在能做什么?好吧,Java为我们提供了try-catch,您可以执行以下操作: try { i = Integer.parseInt(myString); } catch (NumberFormatException e) { e.printStackTrace(); //somehow workout the issue with an improper input. It's up to your business logic. }

435

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧