?
Solved

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

Posted on 2009-05-05
7
Medium Priority
?
6,504 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 400 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 1600 total points
ID: 24302850
String s = t3.getText().trim();
n_id = (s.length()==0 ? 0 : Integer.parseInt(s));
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1600 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 1600 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 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