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
...全文
729 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. }
内容概要:本文研究了一种周期感知LightGBM预测与NSGA-Ⅱ优化耦合的建筑群能源系统协同调度方法,旨在提升建筑群能源系统的低碳经济性和运行效率。该方法首先利用周期感知的LightGBM模型对建筑群的能源负荷与可再生能源出力进行高精度时序预测,充分挖掘数据中的周期性规律,显著提高预测准确性;随后,结合多目标优化算法NSGA-Ⅱ,构建协同调度模型,综合考虑经济成本、碳排放和能源利用率等多个目标,求解得到一组帕累托最优调度方案,实现多目标间的均衡优化。研究通过Python编程实现了完整的预测与优化流程,并在实际场景中验证了所提方法在降低系统运行成本和减少碳排放方面的有效性。; 适合人群:具备一定Python编程基础,从事电力系统、能源管理、智能优化或机器学习相关领域的研究人员与工程技术人员,尤其适合关注多目标优化与时间序列预测融合应用的硕士、博士研究生及科研工作者。; 使用场景及目标:①应用于建筑群、园区等多主体能源系统的协同调度与低碳优化;②实现对光伏、负荷等具有强周期性特征的时间序列数据进行精准预测;③解决能源调度中经济性、环保性等多目标相互冲突的优化问题,获得兼顾各方需求的调度策略。; 阅读建议:此资源以实际代码实现为核心,建议读者在学习过程中结合Python代码,重点理解周期特征提取、LightGBM模型构建、NSGA-Ⅱ算法应用及多目标优化结果分析等关键环节,并尝试在不同场景下复现和调整参数,以深化对方法原理与工程应用的理解。
代码下载地址: https://pan.quark.cn/s/a3d7b3cb0017 ### 利用LabVIEW达成网页数据获取及网页间互动的核心技术要点 #### 一、背景与挑战界定 在当代软件开发领域,从网络页面中获取数据以及与网页进行互动的需求正逐步增长。这一需求不仅适用于数据分析范畴,同时也广泛用于自动化测试、内容采集等多个领域。本文将系统阐述如何借助LabVIEW这一功能强大的可视化编程平台,实现对网络页面数据的采集以及与网页的互动操作。 #### 二、解决策略的制定 为了达成上述目标,必须仔细考虑以下几个核心方面: 1. **如何对网页进行操控**:这包括启动、关闭浏览器窗口,以及加载特定的网络页面等操作。 2. **如何实现与网页的通讯**:这涉及如何读取网页内容,以及如何填写表单数据等任务。 3. **如何提取所需信息**:这需要深入理解HTML的结构,并能够准确定位到特定的数据元素。 #### 三、关键的技术工具 针对上述需求,LabVIEW提供了多种途径来实现网络页面的操控与数据通讯: 1. **IE控件 (Microsoft Internet Controls)**:这是一种基于ActiveX的技术,能够直接在LabVIEW中集成IE浏览器的功能。 2. **IE的自动化对象 (InternetExplorer.Application)**:这是一种COM接口,可以通过调用API来操控IE浏览器。 3. **WebBrowser控件 (.NET控件)**:LabVIEW支持.NET Framework中的WebBrowser控件,该控件提供了一系列API来操控浏览器行为。 4. **HTML文档对象模型 (HTML DOM)**:这是处理...

479

社区成员

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

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