[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check if user has entered only integer

Posted on 2006-04-02
5
Medium Priority
?
198 Views
Last Modified: 2010-03-31
Hi,

I'm using javax.swing.JTextField to get user to enter some data. How can I make sure that user only enters integer for processing?

Thanks
0
Comment
Question by:amai
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 16357148
Hi,

try {
    int val = Integer.parseInt(yourTextField.getText());
    System.out.println("User entered integer value");
}
catch (Exception e) {
    System.out.println("User entered invalid integer value");
    e.printStackTrace();  // I forgot what Exception does it throw, if I am not mistaken, NumberFormatException
}

Thanks
David
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 16357154
Use a custom document with your text field

public class IntegerDocument extends PlainDocument
{
     public IntegerDocument()
     {
          super();
     }

     public void insertString(int offset, String s, AttributeSet attributeSet)
          throws BadLocationException
     {
          try
          {
               Integer.parseInt(getText(0, offset) + s + getText(offset, getLength()-offset));
               super.insertString(offset, s, attributeSet);
          }
          catch (Exception ex)
          {
               Toolkit.getDefaultToolkit().beep();
          }
     
     }
}


and use it like this:

JTextField tf = new JTextField(new IntegerDocument());
0
 

Author Comment

by:amai
ID: 16357186
Hi objects,

I don't really understand why must you use offset, and later insertString. Can you explain?
Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 16357197
offset tells where to insert entered text inteo existing string
and you only want to insert the text if it is successfully paresed as an int ie. if its not an int then don't insert
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16357205
Mmm....

I think that objects proposed you a better solution ;) so use it.

David
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

831 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