简体   繁体   中英

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 :

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 . 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.

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

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.

So you need to trim the whitespaces when you get the String value and then check if the string is empty.

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. 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.

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?

If the empty String means 0 in your case, you can just check this before:

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. If you want to treat it as 0, add the appropriate if statement.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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