0

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

        }
    }
}
2
  • 2
    Please post the whole stacktrace with the linenumber, there you will find the error... Commented May 10, 2011 at 7:10
  • Tobiask : line number is Line 47 and the error is in this line of the code s[i]=Integer.parseInt(cell[i][j].getContents()); Commented May 10, 2011 at 8:32

4 Answers 4

1

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

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

1 Comment

True TJ the line mentioned by you is where the problem lies.The Line number is 47. 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.
0

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.

2 Comments

Vincent my input file has data. I am facing this problem only when i tried to convet it into int. If s[] is a string then there is no problem.
Print it. Even if it has data, it's not int compatible. So just print it.
0

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.

Comments

0

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.