Solved

Double.parseDouble behavior

Posted on 2004-04-02
7
384 Views
Last Modified: 2010-03-31
this is something I don't get...

I declared like this:
minXSinTField = new JTextField("0.0", 4);

and then I am using this func to test input validation.
isValidInput(minXSinTField);

This is the func.
  public boolean isValidInput(JTextField tf) {
    boolean result = true;
    try {
      Double.parseDouble(tf.getText());
      result = true;
      System.out.println("in try section");
      System.out.println(result);
    }
    catch (NumberFormatException ex) {
      result = false;
      System.out.println("in  catch section");
      System.out.println(result);
    }
    System.out.println("before return;");
    System.out.println(result);
    return result;
  }

Now, when I enter any string this func returns true.
From other pane's textfields, when I enter string this func returns false.

I don't know why...
0
Comment
Question by:dkim18
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10741405
Please post input
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10741411
Hi dkim18,


Try this:

  public boolean isValidInput(JTextField tf) {
    try {
      Double.parseDouble(tf.getText());
      return true;
    }
    catch (Exception ex) {
      return false;
    }
  }
0
 

Author Comment

by:dkim18
ID: 10741772
Webstorm,

when I entered string, it still returns true...

CHEJ,

minXSinTField = new JTextField("0.0", 4);

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 13

Accepted Solution

by:
Webstorm earned 125 total points
ID: 10741858
Display the value returned by getText() :

  public boolean isValidInput(JTextField tf) {
    try {
      System.out.println("Input is ("+tf.getText()+")");
      Double.parseDouble(tf.getText());
      return true;
    }
    catch (Exception ex) {
      return false;
    }
  }
0
 

Author Comment

by:dkim18
ID: 10741929
when I enter string "input"
the outcome was:

Input is (0.0)
0
 

Author Comment

by:dkim18
ID: 10741950
I found error.
I am sharing same private members for two panes...
that was why...

thanks anyway,
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10742041
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

23 Experts available now in Live!

Get 1:1 Help Now