所以这一直困扰着我:我在下面的代码中得到了一个数字格式异常(这是一个按钮顺便说一下,正在为学校做一个GUI):
String provider = clientInfo.getMediAidProvider();
String line = "";
int totalCost = treatmentInfo.getTotalCost();
System.out.println("P" + totalCost);
double percentPayout = 0.0;
double totalPayed = 0.0;
double toBePayed = 0.0;
int max = 0;
int min = 0;
try {
Scanner sc = new Scanner(new File(provider + "Momentum Algorithm.txt"));
while (sc.hasNextLine()) {
line = sc.nextLine();
if (line.contains("*")) {
String pPayout = line.substring(line.indexOf("=") + 1, line.indexOf("%"));
percentPayout = Integer.parseInt(pPayout);
totalPayed = totalCost * (percentPayout / 100);
toBePayed = totalCost - totalPayed;
System.out.println("D" + percentPayout);
System.out.println("E" + totalPayed);
System.out.println("?" + toBePayed);
} else {
max = Integer.parseInt(line.substring(line.indexOf("~") + 2, line.indexOf("=") - 1));
min = Integer.parseInt(line.substring(0, line.indexOf("~") - 1));
System.out.println("{" + min);
System.out.println("/" + max);
if (totalCost >= min && totalCost <= max) {
String pPayout = line.substring(line.indexOf("=") + 1, line.indexOf("%"));
percentPayout = Integer.parseInt(pPayout);
totalPayed = totalCost * (percentPayout / 100);
toBePayed = totalCost - totalPayed;
System.out.println("+" + percentPayout);
System.out.println("0" + totalPayed);
System.out.println("*" + toBePayed);
break;
}
}
}
} catch (FileNotFoundException | NumberFormatException e) {
System.out.println(e);
}
tblInvoice.setValueAt("R" + totalCost, 0, 0);
tblInvoice.setValueAt(percentPayout + "%", 0, 1);
tblInvoice.setValueAt("R" + totalPayed, 0, 2);
tblInvoice.setValueAt("R" + toBePayed, 0, 3);
基本上我是引入对象,转换为字符串然后转换为整数或双精度,或者直接从对象转换为整数。这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:507)
at java.text.Format.format(Format.java:157)
at javax.swing.plaf.synth.SynthTableUI$SynthTableCellRenderer.configureValue(SynthTableUI.java:802)
at javax.swing.plaf.synth.SynthTableUI$SynthTableCellRenderer.getTableCellRendererComponent(SynthTableUI.java:789)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
还有更多的位置,但它们实际上都没有链接到我的GUI类或任何其他自定义创建的类。 所以这就是问题:它给了我确切的问题,NumberFormatException,但是它告诉我错误是什么。我已经尝试使用System.out.println来跟踪代码,它似乎运行正常。我不知道什么对象被传递给它不会接受的Integer.parseInt方法或它传递对象的位置。 任何帮助将不胜感激!
答案 0 :(得分:0)
看起来你的tblInvoice是一种表,哪个方法setValueAt接受第一个参数作为指向行的指针&#34; R + number&#34; 对于例子
tblInvoice.setValueAt("R" + totalPayed, 0, 2);
使用double类型totalPayed
totalPayed = totalCost * (percentPayout / 100);
将像这样评估:
tblInvoice.setValueAt("R45.12", 0, 2);
这可能不是一个好的行号
无论如何异常出现在GUI线程中,而不是代码中,所以首先要检查的是你发送给GUI元素的参数