Solved

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

Posted on 2009-05-05
7
6,467 Views
Last Modified: 2013-12-22
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
0
Comment
Question by:jazzIIIlove
  • 4
  • 2
7 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 24302842
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
 
LVL 92

Expert Comment

by:objects
ID: 24302843
>        n_id=Integer.parseInt(t3.getText().trim());

need to check the string isn't empty

0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 24302850
String s = t3.getText().trim();
n_id = (s.length()==0 ? 0 : Integer.parseInt(s));
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 24302963
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 24302980
almost :)

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24303013
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 24303045
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 54
Impossible to extract MSI from new JAVA releases 2 47
replace a word with other 1 40
map related example 6 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now