Solved

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

Posted on 2009-05-05
7
6,456 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
Comment Utility
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
Comment Utility
>        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
Comment Utility
String s = t3.getText().trim();
n_id = (s.length()==0 ? 0 : Integer.parseInt(s));
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Author Comment

by:jazzIIIlove
Comment Utility
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
Comment Utility
almost :)

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

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 86
endX challenge 2 48
mapBully challenge 6 88
What is the latest versions eclipse neon 2 116
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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 video teaches viewers about errors in exception handling.

771 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

12 Experts available now in Live!

Get 1:1 Help Now