8

I have:

 op1 = Integer.parseInt(jTextField1.getText());
 op2 = Integer.parseInt(jTextField2.getText());

However, I want to check first whether the text fields' values can be assigned to integer variables. How do I do that?

I've been going through this for a long time, so, if this was already asked here, forgive me

user
  • 6,897
  • 8
  • 43
  • 79
Dannyl
  • 115
  • 2
  • 3
  • 8
  • `if (op1 == 0)`? I'm not sure what you're asking. – Dave Newton Feb 15 '14 at 21:33
  • That's why I said it's hard to explain. I want to check if I can assign a value to a variable – Dannyl Feb 15 '14 at 21:33
  • 2
    Wait, are you saying that `if((int i = 0) == true)` is valid C code?! – NPE Feb 15 '14 at 21:34
  • If it's not a valid integer you'll get an exception. If you don't get the exception, you can assign it. – Dave Newton Feb 15 '14 at 21:34
  • You can, if you couldn't, the compiler would complain. (potential exception: autoboxing and null values) – qqilihq Feb 15 '14 at 21:34
  • 1
    Hi @user3314478, and welcome to Stack Overflow. I have edited your question according to your comment on one of the answers. If my edit changed the intent of your question, please feel free to [edit] further, or go into the [revision history](http://stackoverflow.com/posts/21803908/revisions) and roll back the edit. Do however note that as it stands, this question is very likely to be closed as a duplicate of some other question on the site. – user Feb 15 '14 at 21:41
  • See [5660595] (http://stackoverflow.com/questions/5660595/simple-integer-regular-expression) – Jonathan Rosenne Feb 15 '14 at 21:58
  • @JonathanRosenne Um, no. That's ASP.NET, this is Java. It's also about regular expressions, which I definitely don't think the OP here needs. Remember the old adage; you have a problem, and think "I know, I'll use regular expressions" - now you have two problems. While regexes are useful in certain scenarios, they have serious drawbacks as well. – user Feb 15 '14 at 22:22

4 Answers4

16

You can't do if (int i = 0), because assignment returns the assigned value (in this case 0) and if expects an expression that evaluates either to true, or false.

On the other hand, if your goal is to check, whether jTextField.getText() returns a numeric value, that can be parsed to int, you can attempt to do the parsing and if the value is not suitable, NumberFormatException will be raised, to let you know.

try {
    op1 = Integer.parseInt(jTextField1.getText());
} catch (NumberFormatException e) {
    System.out.println("Wrong number");
    op1 = 0;
}
Warlord
  • 2,798
  • 16
  • 21
  • 1
    The use of an exception as a branching mechanism is discouraged. If the input is likely to be malformed, an explicit validity check should be performed rather than exception catching. – Witiko Dec 19 '14 at 09:25
  • Typo: `NumericFormatException` should be `NumberFormatException`. – Gerrit Brouwer Mar 19 '15 at 09:10
2

This works for me. Simply to identify whether a String is a primitive or a number.

private boolean isPrimitive(String value){
        boolean status=true;
        if(value.length()<1)
            return false;
        for(int i = 0;i<value.length();i++){
            char c=value.charAt(i);
            if(Character.isDigit(c) || c=='.'){
                
            }else{
                status=false;
                break;
            }
        }
        return status;
    }
1

parseInt throws NumberFormatException if it cannot convert the String to an int. So you should surround the parseInt calls with a try catch block and catch that exception.

xp500
  • 1,426
  • 7
  • 11
0

Basically, you have to decide to check if a given string is a valid integer or you simply assume a given string is a valid integer and an exception can occur at parsing.

Smutje
  • 17,733
  • 4
  • 24
  • 41