Solved

JTextField Exception

Posted on 2004-10-18
4
382 Views
Last Modified: 2008-03-17
I have a JTextField and an enter button. The user will enter a number into the JTextfield and hit enter when he is done.

What is the best way to read and write to a JTextField box. I want to limit the input field to numbers
between 1 -100. The problem I have is if the field is empty whent the enter button is pushed I receive an exception.

Here is my code:
        Object oHOTValue;
        try
        {
            oHOTValue = heightOfTargetTextField.getText();
        }
        catch (NumberFormatException s )
        {
            oHOTValue = "1";
        }
        String sHOTValue = oHOTValue.toString();
        int iHOTValue = Integer.parseInt(sHOTValue);
        zoomConfig.setTargetHeight( iHOTValue );
        iHOTValue = zoomConfig.getTargetHeight();
        String sValue = Integer.toString(iHOTValue);
        heightOfTargetTextField.setText(sValue);
        System.out.println("HOT button pressed : " + iHOTValue);

The try catch is not working correctly.

thanks

0
Comment
Question by:hugoitt
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 total points
ID: 12341186
Hi hugoitt,

The try-catch clause is in the wrong location!

        String sHOTValue = heightOfTargetTextField.getText();
        int iHOTValue = 1;
        try
        {
            iHOTValue = Integer.parseInt(sHOTValue);
        }
        catch (NumberFormatException s )
        {
            // already has default value
        }
        zoomConfig.setTargetHeight( iHOTValue );
        iHOTValue = zoomConfig.getTargetHeight();
        String sValue = Integer.toString(iHOTValue);
        heightOfTargetTextField.setText(sValue);
        System.out.println("HOT button pressed : " + iHOTValue);

\t
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12341194
The try-catch is working correctly.
Why don't you:

1)  String input = textfield.getText();
2)  int value;
     try
     {
           value = Integer.ParseInt(input);
     }
     catch(NumberFormatException e)
     {
..... your action on bad input ....
     }

;JOOP!
0
 

Author Comment

by:hugoitt
ID: 12341474
thanks grangehead911
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12341626
=-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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…

758 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