?
Solved

JTextField Exception

Posted on 2004-10-18
4
Medium Priority
?
392 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 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