2

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)
1
  • 2
    "calcul" Is that a word in your language, or are you just being lazy? Commented Sep 2, 2011 at 10:15

8 Answers 8

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please do keep in mind that adding a try...catch block on the innermost part of a loop can be expensive on Java; explicitly checking for non-emptiness will be probably more efficient, as long as you don't have any cells with non-numeric non-empty text.
2

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

Comments

2

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.

Comments

1

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

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?

Comments

1

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

Comments

0

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

Comments

0

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!

Comments

0

You could use Java Optionals to accomplish that:

public class NumberUtil {
    
    public static Integer convertBlankOrNull(String string) {
        return Optional.ofNullable(string)
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Integer::parseInt)
                .orElse(0);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.