简体   繁体   English

java:将字符串值转换为int

[英]java : convert string value to int

from JTable , I try to get values (which are string) of each column from [row 1 to ..end of row] and calcul sum of values as follow : JTable开始 ,我尝试从[行1到行的末尾]获取每列的值(字符串),并计算值的总和如下:

final ArrayList<String>ValuesList = new ArrayList<String>();
final int nb = myTable.getRowCount();
int sum=0;

for (int i = 1; i < nb; i++)
{
    String columnValue = myTable.getColumnValue(i, columnName);

    sum=sum+Integer.parseInt(columnValue);

    ValuesList .add(columnValue);
}

but I got : 但我得到了:

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)

You have at least one empty cell in your table. 您的表中至少有一个空单元格。

Here's a suggestion: 这是一个建议:

try {
   sum += Integer.parseInt(columnValue);
} catch (NumberFormatException nfe) {
  // the cell is either empty or not an integer number
  // will be treated as zero (0)
}

Note, that the method getColumnValue(int i, String name) is not defined for javax.swing.JTable . 注意,没有为javax.swing.JTable定义方法getColumnValue(int i, String name) If you use a subclass of JTable then the error could be in that class/method too: it may return an empty string instead of a cell value. 如果你使用JTable的子类,那么错误也可能在那个类/方法中:它可能返回一个空字符串而不是一个单元格值。

Put a check for null/empty string before calling the conversion with parseInst . 在使用parseInst调用转换之前检查null /空字符串。

if (columnValue != null && !columnValue.isEmpty())
    sum=sum+Integer.parseInt(columnValue);

I am not sure about exact syntax so verify before using it. 我不确定确切的语法,因此请在使用之前进行验证。

I guess you will need to convert an empty String or null value to zero. 我想你需要将空String或null值转换为零。

So you need to trim the whitespaces when you get the String value and then check if the string is empty. 所以你需要在获得String值时修剪空格,然后检查字符串是否为空。

Here is what your code should look like 这是您的代码应该是什么样子

final ArrayList<String>ValuesList = new ArrayList<String>();
final int nb = myTable.getRowCount();
int sum=0;
String columnValue = "";

for (int i = 1; i < nb; i++)
{
    columnValue = myTable.getColumnValue(i, columnName).trim();
    if (!columnValue.isEmpty()) {
        sum=sum+Integer.parseInt(columnValue);
    }

    ValuesList.add(columnValue);
}

If columnValue is empty ( "" ), you can't parse it. 如果columnValue为空( "" ),则无法解析它。 Just skip the that column in this case, ie 在这种情况下,只需跳过该列,即

if( columnValue != null || columnValue.length() > 0 ) {
 //parse and add here
}

Note that I added the check for null just in case, you might not need it if myTable.getColumnValue(...) is guaranteed to never return null. 请注意,为了以防万一,我添加了对null的检查,如果myTable.getColumnValue(...)保证永远不会返回null,则可能不需要它。

Also note that you might try and handle other cases as well, depending on what values might be in your table. 另请注意,您也可以尝试处理其他情况,具体取决于表中可能包含的值。 If blank strings like " " or general alpha-numeric values are allowed, you need to account for that as well. 如果允许使用空白字符串(如" "或一般字母数字值,则还需要考虑这一点。

Finally, why are your values stored as strings if they actually are numbers? 最后,为什么你的值存储为字符串,如果它们实际上是数字? Why don't you store them as Integer objects right away? 为什么不立即将它们存储为Integer对象?

If the empty String means 0 in your case, you can just check this before: 如果空字符串在您的情况下意味着0,您可以在之前检查:

if (!columnValue.equals(""))
    sum=sum+Integer.parseInt(columnValue);

Or even better: 甚至更好:

if(!columnValue.isEmpty())
    sum=sum+Integer.parseInt(columnValue);

An empty string cannot be converted to an int. 空字符串无法转换为int。 If you want to treat it as 0, add the appropriate if statement. 如果要将其视为0,请添加适当的if语句。

What the compiler is telling you is that you are trying to convert an empty string "" to an int . 编译器告诉你的是你正在尝试将空字符串""转换为int So you may want to check that you are converting strings that actually represent integers! 所以你可能想检查你是否正在转换实际代表整数的字符串!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM