Solved

Double.parseDouble behavior

Posted on 2004-04-02
7
385 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

920 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

17 Experts available now in Live!

Get 1:1 Help Now