Solved

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

Posted on 2009-05-05
7
6,486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 74
Error in @AspectJ Based AOP with Spring 2 24
Running JavaFX on JDeveloper 12C 1 79
What is the use of Forwarding Class in java 1 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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