2

I have a JTextField object that a user can type in a number(or anything) into. I am trying to parse this textfield for a number and I do a validation check to see if the number is within a certain range (1-999).

String lowerLimit = this.lowerLimitTextField.getText().trim();
String upperLimit = this.upperLimitTextField.getText().trim();

if( Integer.parseInt(lowerLimit) < 1 || Integer.parseInt(upperLimit) > 999 )
{
    return "The string must be in the range 0-999";
}

My issue is that, the user can specify any value into the textfield. When I try to input something like "61412356123125124", I get a NumberFormatException when parsing the string. What would be the simplest way to handle such a case? I do have a check that makes sure that the inputted string is all numbers, so thats fine.

I have tried changing the parseInt() into a parseLong() but I still get the same issue, since the number inputted is essentially unbounded. Can this be done with parsing the string (preferred), or is the simplest way to set some constraints on the JTextField itself?

4
  • 4
    1 - Check string length. 2 - Then parse and do equality. Commented Sep 19, 2014 at 14:48
  • One way -- use an InputVerifier, another way, use a JFormattedTextField, another way, use a DocumentFilter. Whatever you do, don't use a KeyListener for any of this as you'll risk hard to debug errors. Commented Sep 19, 2014 at 14:49
  • Although unlikely, what if the user put ins something like "0000235"? String length would determine this to be incorrect, but it technically should be accepted. Commented Sep 19, 2014 at 14:49
  • What C.B said or use a BigInteger object, but thats a lot more expensive (accounts for obscure inputs though). Commented Sep 19, 2014 at 14:49

3 Answers 3

3

Use NumberFormatto parse

import java.text.NumberFormat;
import java.text.ParseException;

public class MyVisitor {


    public static void main(String[] args) throws ParseException {

        System.out.println(NumberFormat.getNumberInstance().parse("61412356123125124"));
    }
}

outputs

61412356123125124
Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you do not want to get the number, just check range (0-999). In this case just catch NumberFormatException and return same string:

try {
    if( Integer.parseInt(lowerLimit) < 1 || Integer.parseInt(upperLimit) > 999 ) {
        return "The string must be in the range 0-999";
    }
} catch (NumberFormatException e) {
        return "The string must be in the range 0-999";
        //or more relevant message, like "Number to long" or something
}

2 Comments

A lot of calls to the same method and you never actually return the value. Apart from that, throw errors or print warnings instead of returning strings.
just added some to existing code, question was for the solution, not improving implementation
0

The answer is that the Exception is your friend: It's telling you the number is too large... Put the error handling in the catch block or better yet declare throws NumberFormatException and catching calling block so that the method can be recalled

So you can use Integer.parseInt, Long.parseLong, or new BigInteger(String)... I would recommend Integer.parseInt in this case. Once you've got an int, you can do bounds checking. And if it's out of bounds, you might just want to throw an NumberFormatException :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.