Tag: numberformatexception

NumberFormatException:无限或NaN

我有一个方法,取n并返回第n个斐波纳契数。 在方法实现中我使用BigDecimal来获取第n个Fibonacci数,然后我使用方法toBigInteger()来获取作为BigInteger对象的数字,这肯定是因为我在我的应用程序中处理大量数字。 我一直得到正确的结果,直到我通过1475作为我的方法的参数。 我得到NumberFormatException: Infinite or NaN在这种情况下NumberFormatException: Infinite or NaN没有任何明确的理由。 你能解释一下我为什么会得到这个例外吗? 这是我的方法: BigInteger getFib(int n){ double phi = (1 + Math.sqrt(5))/2; double squareRoot = (Math.sqrt(5)) + (1/2); BigDecimal bd = new BigDecimal(Math.floor(Math.pow(phi, n)/(squareRoot))); return bd.toBigInteger(); }

将字符串arraylist存储到双arraylist中?

我有一个字符串arraylist,其数字如[“51,073”,“51,074”,“51,073”]现在我想将其转换为Double ArrayList并计算数组列表中数字的总和。 这是代码: private ArrayList totals = new ArrayList(); private ArrayList demototal = new ArrayList(); String total = itemData.getString(ParseBarcode.KEY_TOTAL); totals.add(total); /*converting all values from totals array to Double and add to arraylist demototal*/ for (int i = 0; i < totals.size(); i++) { final String value = totals.get(i); double total_ary = Double.parseDouble(value); demototal.add(total_ary); } String […]

输入大数字以避免NumberFormatException

我必须控制从1到9999999999的输入(表示学校的注册号)原始类型long只能容纳大约2.000.000.000,我该如何存储这个大小的数字?

尝试从.getText()解析字符串时出现NumberFormatException;

Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “Total: 8.78” at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at PizzaOrder$BtnClicked.(PizzaOrder.java:298) at PizzaOrder$BtnClicked.(PizzaOrder.java:295) at PizzaOrder.addListeners(PizzaOrder.java:102) at PizzaOrder.(PizzaOrder.java:76) at PizzaDriver$1.run(PizzaDriver.java:46) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown […]

Integer.parse(String str)java.lang.NumberFormatException:错误

我不断得到数字格式的期望,即使我正在修剪字符串并且它们不包含非数字字符,奇怪的是它适用于某些数字,而不适用于其他数字。 下面是一个我得到数字格式exception的字符串示例。 此外,任何以0开头的字符串,例如“0208405223”,返回208405223,不再有零是应该发生的吗? String n=”3020857508″; Integer a = Integer.parseInt(n.trim()); System.out.println(a); 这是例外: Exception in thread “main” java.lang.NumberFormatException: For input string: “3020857508” at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:583) at java.lang.Integer.parseInt(Integer.java:615) at JavaBeans.Main.main(Main.java:15)

BigDecimal和BigInteger的限制是什么?

我在一个程序中将两个巨大的BigInteger值相乘。 它失败了。 BigInteger和BigDecimal的限制是什么?

无法有效地使用Java中的Multi Catch

我真的想使用Java-1.7的function。 其中一个function是“Multi-Catch”。 目前我有以下代码 try { int Id = Integer.parseInt(idstr); TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id)); updateTotalCount(tempTypeInfo); } catch (NumberFormatException numExcp) { numExcp.printStackTrace(); } catch (Exception exception) { exception.printStackTrace(); } 我想从上面的代码中删除两个catch块,而是使用如下所示的单个catch: try { int Id = Integer.parseInt(idstr); TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id)); updateTotalCount(tempTypeInfo); } catch (Exception | NumberFormatException ex) { // –> compile time error ex.printStackTrace(); } 但上面的代码给出了编译时错误: “NumberFormatException”已被替代Exception捕获。 […]

“D”和“F”的数字格式exception的例外情况?

我的代码中遇到了一个非常奇怪的问题。 我有一个简单的温度转换器,用户输入摄氏温度,按“转换”后,显示华氏温度。 如果用户未输入有效的内容(任何不是数字或小数的内容),则会显示错误对话框。 码: btnConvert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String tempFahr = (String) enterDegreesC.getText(); double tempF = Double.valueOf(tempFahr); double tempFConverted = tempF * 1.8 +32; displayDegreesF.setText(tempFConverted + ” Farenheit”); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(frmTemperatureConverter, “Please Enter a Number.”, “Conversion Error”, JOptionPane.ERROR_MESSAGE); } } }); 相当直接和简单的代码,除了一件事,效果很好。 当我输入数字后跟字母“f”或“d”的组合时,不显示错误对话框,并使用字母前面的数字计算华氏温度。 这只发生在“d”和“f”(以及“D”和“F”)而不是任何其他字母。 我很难过这个。 为什么只有这两个字母放在一个数字后会导致exception不被抛出并继续计算?

怎么回事?(NumberFormatException:null)

import java.io.*; class AccountInfo { private String lastName; private String firstName; private int age; private float accountBalance; protected AccountInfo(final String last,final String first,final int ag,final float balance) throws IOException{ lastName=last; firstName=first; age=ag; accountBalance=balance; } public void saveState(final OutputStream stream){try{ OutputStreamWriter osw=new OutputStreamWriter(stream); BufferedWriter bw=new BufferedWriter(osw); bw.write(lastName); bw.newLine(); bw.write(firstName); bw.write(age); bw.write(Float.toString(accountBalance)); bw.close();} catch(IOException e){ System.out.println (e); […]

Java中的parseDouble导致NumberFormatException

我试图从属性文件加载信息,我有以下代码: anInt = Integer.parseInt(prop.getProperty(“anInt”)); aDouble = Double.parseDouble(prop.getProperty(“aDouble”)); 虽然第一行工作正常,我试图加载双变量的第二行抛出NumberFormatException 。 具体的exception消息是: Exception in thread “main” java.lang.NumberFormatException: For input string: “78,5” at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222) at java.lang.Double.parseDouble(Double.java:510) at Assignment1.BaseStation.readPropertyFile(BaseStation.java:59) at Assignment1.BaseStation.main(BaseStation.java:83)