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

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

Posted on 2009-05-05
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...
Question by:jazzIIIlove
  • 4
  • 2
LVL 86

Assisted Solution

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)
LVL 92

Expert Comment

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

need to check the string isn't empty

LVL 92

Accepted Solution

objects earned 400 total points
ID: 24302850
String s = t3.getText().trim();
n_id = (s.length()==0 ? 0 : Integer.parseInt(s));
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 12

Author Comment

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

Assisted Solution

objects earned 400 total points
ID: 24302980
almost :)

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

LVL 86

Expert Comment

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

LVL 92

Assisted Solution

objects earned 400 total points
ID: 24303045
If you really want to check if its an int then use the following


expensive operation though and not necessary in your case.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 82
how to see all occupied ports on windows 10 laptop 15 75
ejb example issues 3 19
ejb on wildfly 5 30
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

809 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