lamisse
lamisse

Reputation: 33

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)

Upvotes: 2

Views: 20307

Answers (8)

Nakamoto
Nakamoto

Reputation: 1386

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

Upvotes: 0

Aditya
Aditya

Reputation: 116

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.

Upvotes: 2

Alexius DIAKOGIANNIS
Alexius DIAKOGIANNIS

Reputation: 2584

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

Upvotes: 2

Simson
Simson

Reputation: 824

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

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114767

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.

Upvotes: 4

Thomas
Thomas

Reputation: 88707

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?

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53819

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!

Upvotes: 0

Thilo
Thilo

Reputation: 262504

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

Upvotes: 0

Related Questions