Klausos Klausos
Klausos Klausos

Reputation: 16050

How to process empty fields in integer column of JTable?

I have a column in a table that contains numeric values. However, some fields are empty. I want to read all fields in this column into integer variable. How should I manage empty fields?

int total = tableModel.getValueAt(currentRow, currentCol).toString().equals("") ? 0 : Integer.parseInt(tablePaxModel.getValueAt(currentRow, currentCol).toString());

Upvotes: 1

Views: 658

Answers (2)

AbtPst
AbtPst

Reputation: 8008

something like

try {
    String val = tablePaxModel.getValueAt(currentRow, currentCol).toString();

    int temp=0;
    if(val.isEmpty() || val == null)
       temp=0;

    else
    {
    temp = Integer.parseInt(val);
    }

    total = total + temp
}

catch (Exception e) 
    {
        e.printStackTrace();  
    }

something like that. i have taken some assumptions with your code but you can modify as needed

Upvotes: 0

cno
cno

Reputation: 184

     try 
     {
        String valueInCell = (String)tablePaxModel.getValueAt(currentRow, currentCol);

        if(valueInCell == null || valueInCell.isEmpty())
        {
            valueInCell = "0";
        }

        int tempCellValue = Integer.parseInt(valueInCell);

        total += tempCellValue;

    } catch (Exception e) 
    {
        e.printStackTrace();  
    }

Upvotes: 2

Related Questions