12 Replies Latest reply on Nov 23, 2008 4:25 PM by 800298

# getting an integer from an input box

the title pretty much covers it, i basically need to assign an integer value given from and input box to a variable.

i have search for example of this, but only found examples for returning strings, i tried to adapt it, but i couldnt figure it out.

can anyone please give me an exampel of a simple (very simple) input box that does this?

thank you
• ###### 1. Re: getting an integer from an input box
Hello Flossy

[Have a gander at this lil beauty!|http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html]
• ###### 2. Re: getting an integer from an input box
Simple: Get a String from the input box, and then convert it with [Integer.parseInt|http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)]. No need for bling.
• ###### 3. Re: getting an integer from an input box
No need for bling.
Excuse me?! I can't believe what I'm hearing here. Next thing you'll be wearing my Rolex.
• ###### 4. Re: getting an integer from an input box
I have a perfectly servicable lorus, water proof to 300m, apparently.
• ###### 5. Re: getting an integer from an input box
If you're reading from the console, use this application as an example for using the Integer.parseInt method:
``````import java.io.BufferedReader;

public static void main(String args[]) {
System.out.println("Type a number");
}
}``````
• ###### 6. Re: getting an integer from an input box
abraham21190 wrote:
If you're reading from the console, use this application as an example for using the Integer.parseInt method:
``````import java.io.BufferedReader;

public static void main(String args[]) {
System.out.println("Type a number");
}
}``````
See: [http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#nextInt()]
But I do not think that the OP was using the console. Talking about swing and textFields. -> I would follow the link Keith provided.
• ###### 7. Re: getting an integer from an input box
i took a look at the link edward provided but i couldnt make anything of it, we havent covered swing in out lectures yet.

howeveri have been able to use simpe thigs such as msg boxs and yes no boxes, so im just tryingto be consistant.

just a simple example of an input box reading an integer would be most helpful.

thank you
• ###### 8. Re: getting an integer from an input box
You said you already have the string from the input box. So read reply #2 and do
``int number = Integer.parseInt(msgBoxOutputString);``
It's not the Rolex way, but you can learn that later. ;)
• ###### 9. Re: getting an integer from an input box
whats the Rolex way?
• ###### 10. Re: getting an integer from an input box
The first link I posted gives you a way to prevent the user from entering anything other than a number in the box, thus preventing any parse errors.

It's the best solution but probably too advanced for you at this stage. The simpler way is perfectly serviceable but doesn't have the bling, as Keith says!
• ###### 11. Re: getting an integer from an input box
well actually the problem is that i need to avoid parse errors. i'll take another look

EDIT

actually is there a way to implementing input validation to the text box?
• ###### 12. Re: getting an integer from an input box
[http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#inputVerification|http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#inputVerification]

In the input verifier example there is a lot of code there to digest but really all it is doing is parsing the number in the field before the value of the field is updated. If there is a ParseException thrown then the user input is rejected and the value of the text field doesn't change.

You could implement this more simply by catching the ParseException and popping up the box again until the user types a valid entry.

e.g:
``````boolean validInput = false
while (!validInput) {
try {
//pop up and parse input msg etc
validInput = true;
catch (ParseException e) {
//msg to user -> invalid input
}
}``````
Obviously not as nice but it gets the job done. We'll call this the 300m Lorus solution.

Edited by: Edward_Kimber on Nov 23, 2008 8:22 AM