Solved

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

Posted on 2009-05-05
7
6,493 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
Technology Partners: 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

690 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