user737634
user737634

Reputation: 1

Runtime Error in Java code "Exception in thread "main" java.lang.NumberFormatException: For input string: "

I am facing this runtime error **"Exception in thread "main" java.lang.NumberFormatException: For input string: "" "** Please help me out to overcome this.

Here is the code snipet :

package SampleC;
import java.io.File;
import java.io.IOException;
import java.util. *;


import jxl.*;
import jxl.read.biff.BiffException;


import jxl.write.WriteException;

import java.util.ArrayList;
import java.util.Vector;

public class Test {
    public static void main(String args[])throws IOException, BiffException, WriteException {
    {
        try
        {
        Workbook workbook=Workbook.getWorkbook(new File("C:\\Documents and Settings\\snandam\\Desktop\\readvalues.xls"));

         Sheet sheet =workbook.getSheet(0);
    // String[] s=new String[200];
         int[] s = new int [200];
         Cell[][] cell=new Cell[100][100];

         ArrayList<Integer> myList = new ArrayList<Integer>();
         {


         for(int i=0;i<sheet.getColumns();i++)
         {
             for(int j=0;j<sheet.getRows(); j++)
             {
             cell[i][j] = sheet.getCell(i, j);

             s[i]=Integer.parseInt(cell[i][j].getContents());

           //  System.out.printf("%s\n", s[i]);
             Collections.addAll(myList, s[i]); 


             } 



      }


      ArrayList<Integer> a1 = new ArrayList<Integer>();
      a1.add(3);a1.add(-3);a1.add(-8);a1.add(0);

      ArrayList<Integer> a2 = new ArrayList<Integer>();
      a2.add(-1);a2.add(-4);a2.add(-7);a2.add(6);

      ArrayList<Integer> a3 = new ArrayList<Integer>();
      a3.add(1);a3.add(5);a3.add(6);a3.add(7);

      ArrayList<Integer> a4 = new ArrayList<Integer>();
      a4.add(-10);a4.add(-4);a4.add(-1);a4.add(3);a4.add(8);

      ArrayList<Integer> a5 = new ArrayList<Integer>();
      a5.add(17);a5.add(18);a5.add(19);a5.add(20);a5.add(21);a5.add(22);a5.add(23);a5.add(24);

      int target = 0;

      Vector<ArrayList<Integer>> vecOfLst   = new Vector<ArrayList<Integer>>();
      vecOfLst.add(myList);
      vecOfLst.add(a1);
      vecOfLst.add(a2);
      GlobalMembers gMem    = new GlobalMembers();
      gMem.findtarget(target, vecOfLst, 3);
      vecOfLst.add(a3);
      vecOfLst.add(a4);
      vecOfLst.add(a5);
      gMem.findtarget(target, vecOfLst, 6);
    }
} catch (BiffException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}  

        }
    }
}

Upvotes: 0

Views: 4024

Answers (4)

T.J. Crowder
T.J. Crowder

Reputation: 1074505

I assume the problem must be on this line:

s[i]=Integer.parseInt(cell[i][j].getContents());

...caused by the return value of cell[i][j].getContents() being non-numeric (perhaps blank).

As @Tobiask points out, the stack trace in a debug build should include the exact line number of the problem, but that was the only parseInt I saw.

Update after your question below:

So what is the solution for this ?? N cell[i][j].getContents() is not empty as data is present and its featching.I guess the probelm is there only when its conveting into int,but then 'm not sure.

Yes, the exception is telling you that the string returned by cell[i][j].getContents() has characters that parseInt considers invalid. Note that the docs for parseInt say:

The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value.

So for instance, a space at the beginning of the string is invalid. A blank string is invalid. Walk through the code with a debugger and see what's in the string (from the exception you reported, the string would seem to be blank, but...).

Upvotes: 1

Niroshan Abayakoon
Niroshan Abayakoon

Reputation: 921

Just check the cell[i][j] value equals null or empty. If cell[i][j]!=null or empty parse the value to int. Then handle the exception .

Upvotes: 0

Ankit
Ankit

Reputation: 2753

Before parsing this line

s[i]=Integer.parseInt(cell[i][j].getContents());

check s[i] for empty string or null or any alphabetic or special characters.

If the above characters are not found then you can parse it.

Upvotes: 0

Vicente Plata
Vicente Plata

Reputation: 3380

There's a time when

cell[i][j].getContents()

returns an empty String. You should check carefully your input file. Maybe debug it by printing what are you trying to parse. Also, please edit your question so the code is more readable.

Upvotes: 0

Related Questions