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

8
  • if (op1 == 0)? I'm not sure what you're asking. Commented Feb 15, 2014 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 Commented Feb 15, 2014 at 21:33
  • 2
    Wait, are you saying that if((int i = 0) == true) is valid C code?! Commented Feb 15, 2014 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. Commented Feb 15, 2014 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 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. Commented Feb 15, 2014 at 21:41

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Typo: NumericFormatException should be NumberFormatException.
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;
    }

Comments

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.

Comments

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.

4 Comments

I want to check if the string is a valid integer
Then please search SO for yourself, this topic is as old as Java.
You could have aswered the question instead of complaining .-.
Part of asking a question in a forum is thinking about the abstract problem behind the question - and if you're not able to type in "java check string integer" in the search box, sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.