gpt4 book ai didi

java.lang.NumberFormatException : For input string: "8999999999999995"

转载 作者:行者123 更新时间:2023-12-01 19:53:02 25 4
gpt4 key购买 nike

我在运行程序时遇到问题。问题出在第 460 行,我将字符串解析为 int。这是代码

       try {
PreparedStatement ps = con.prepareStatement("SELECT water_time, price\nFROM `watering` \nWHERE water_date BETWEEN ? AND ?");
double pricesum = 0;
double timesum = 0;
ps.setDate(1, new java.sql.Date(sDate.getTime()));
ps.setDate(2, new java.sql.Date(eDate.getTime()));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
double time = Double.parseDouble(rs.getString("water_time"));
double price = Double.parseDouble(rs.getString("price"));
pricesum = pricesum + price;
timesum = timesum + time;
}
String totalprice = String.valueOf(pricesum);
String totaltime = String.valueOf(timesum);
String[] totaltime2 = totaltime.split("\\.");
int[] intArr = new int[2];
intArr[0] = Integer.parseInt(totaltime2[0]);
(line 460) intArr[1] = Integer.parseInt(totaltime2[1]);
double price_hour = intArr[0] * 5;
double price_min = (intArr[1] * 5) / 60.0D;
double price = (price_hour + price_min);
jLabel8.setText((totalprice + " €"));
jLabel7.setText((totaltime + " Ώρες"));
jLabel12.setText((price+ " €"));

问题

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "8999999999999995"
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 MainForm.jButton13ActionPerformed(MainForm.java:460)
at MainForm.access$000(MainForm.java:19)
at MainForm$1.actionPerformed(MainForm.java:135)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)

你能明白问题出在哪里吗?预先感谢您。

最佳答案

该数字不适合 int,请使用 long 代替:https://cs.fit.edu/~ryan/java/language/java-data.html

更多信息请参见 Java Docs (强调我的):

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  1. The first argument is null or is a string of length zero.
  2. The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  3. Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
  4. The value represented by the string is not a value of type int.

关于java.lang.NumberFormatException : For input string: "8999999999999995",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50656137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com