Solved

JTextField Exception

Posted on 2004-10-18
4
389 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
[X]
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
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

691 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