java.lang.NumberFormatException: For input string: "" problem...

Hi there;

What is the problem with the following code? i am getting number format exception but the code seems true...How to fix it without haing drastic changes?

As snippet:

Best regards...
Student.txt
StudentEx.txt
LVL 12
jazzIIIloveAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
String s = t3.getText().trim();
n_id = (s.length()==0 ? 0 : Integer.parseInt(s));
0
 
CEHJConnect With a Mentor Commented:
You need to check that the contents of 't3' is a valid number before attempting to parse it or catch the exception (the former is better)
0
 
objectsCommented:
>        n_id=Integer.parseInt(t3.getText().trim());

need to check the string isn't empty

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
jazzIIIloveAuthor Commented:
sorry:
objects:
>>String s = t3.getText().trim();
>>n_id = (s.length()==0 ? 0 : Integer.parseInt(s));

Below is same as above? (I am not used to that ? format)...Could you clarify?

if(s.length() == 0)
            n_id = Integer.parseInt(s);
        else n_id = 0;
0
 
objectsConnect With a Mentor Commented:
almost :)

if(s.length() == 0)
  n_id = 0;
else
   n_id = Integer.parseInt(s);

0
 
CEHJCommented:
The easiest way to avoid exceptions of any kind would probably be
if ("" + t3.getText().matches("\\d+")) {
    // valid
}
else {
   // don't bother trying
}

Open in new window

0
 
objectsConnect With a Mentor Commented:
If you really want to check if its an int then use the following

http://helpdesk.objects.com.au/java/how-to-check-if-a-string-is-an-integer-using-a-regular-expression

expensive operation though and not necessary in your case.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.