java:将字符串值转换为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7281875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:23:42  来源:igfitidea点击:

java : convert string value to int

java

提问by lamisse

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,我尝试从 [row 1 to ..end of row] 获取每列的值(它们是字符串)并计算值的总和如下:

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)

回答by Andreas Dolk

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. If you use a subclass of JTablethen the error could be in that class/method too: it may return an empty string instead of a cell value.

请注意,该方法getColumnValue(int i, String name)不是为 定义的javax.swing.JTable。如果您使用的子类,JTable那么错误也可能在该类/方法中:它可能返回空字符串而不是单元格值。

回答by Aditya

Put a check for null/empty string before calling the conversion with parseInst.

在调用转换之前检查空/空字符串parseInst

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

I am not sure about exact syntax so verify before using it.

我不确定确切的语法,因此请在使用前进行验证。

回答by Alexius DIAKOGIANNIS

I guess you will need to convert an empty Stringor null value to zero.

我猜您需要将空字符串或空值转换为零。

So you need to trim the whitespaces when you get the Stringvalue 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);
}

回答by Simson

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);

回答by Thomas

If columnValueis empty (""), you can't parse it. Just skip the that column in this case, i.e.

如果columnValue为空 ( ""),则无法解析它。在这种情况下只需跳过该列,即

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 Integerobjects right away?

最后,如果您的值实际上是数字,为什么将它们存储为字符串?为什么不立即将它们存储为Integer对象?

回答by Thilo

An empty string cannot be converted to an int. If you want to treat it as 0, add the appropriate if statement.

空字符串不能转换为 int。如果要将其视为 0,请添加相应的 if 语句。

回答by Jean Logeart

What the compiler is telling you is that you are trying to convert an empty string ""to an int. So you may want to check that you are converting strings that actually represent integers!

编译器告诉您的是您正在尝试将空字符串""转换为int. 因此,您可能需要检查是否正在转换实际表示整数的字符串!