0

I have this code:

    kb.useDelimiter("\\b*[\\s]*");
    int answer = 0;
    String num1, num2;
    char operator;

    System.out.print("Enter calculation: "); // user will input **1+6**

    num1 = kb.nextInt();
    operator = kb.next().charAt(0);
    num1 = kb.nextInt();

    switch(operator)
    {
        case '+':
            if (num1.hasNextInt)
            {
                 int aa= Integer.parseInt(num1);
                 int bb= Integer.parseInt(num2);
                answer = CalculatorMethods.add(aa,bb);
            }
            else
            {
                 //converts the String into Double
            }
            break;
    }

What im trying to do in this code is, the user can input in the program directly like this 1+1 . And what i did is i separate each so it will have 1, + and 1... and if the user inputs an integer number.... then i will convert it to an integer and then calls the method.... else it will convert the String into a double and will then call the method..

So, when i compile the code above, all i get is MANY ERRORS like incompatible types, cannot find symbol hasNextInt.... what is wrong in my code?

ANY HELP WILL BE APPRECIATED.

3
  • i change it to what you said.... but it still gives an "cannot find symbol hasNextInt()" error Commented Mar 2, 2015 at 12:34
  • num1 & num2 are strings. You try to set there an int. (nextInt). And num1&num2 dont have hasNextInt() method. Many strange things there. Commented Mar 2, 2015 at 12:34
  • Can you post your full code? Commented Mar 2, 2015 at 14:27

3 Answers 3

1

Your num1 variable is declared as a String, but you are trying to assign an int to it (i'm assuming kb is an instance of a Scanner). Then you are trying to invoke num1.hasNextInt() which is not a method of a String - i think you meant to say kb.hasNextInt().

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

Comments

1

nextInt() returns int so you can not do num1 = kb.nextInt();

neither String or int have hasNextInt

You must declare num1 and num2 as int.

And some changes in switch case:

case '+':

                answer = CalculatorMethods.add(num1,num2);
          break;

2 Comments

if i change something like that in the switch case then i wont be able to know if the user inputs an integer or a double
First of all look on the Scanner class. You are confusing me. pls take a look at this docs.oracle.com/javase/7/docs/api/java/util/…
0

Create one API that will tell you your string is integer or double type

public boolean isInteger(String value) { 
    try {
        Integer.parseInt(value);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

Then,

if (isInteger(num1) && isInteger(num2)){ // then check if it is integer do integer operation
   int aa= Integer.parseInt(num1);
   int bb= Integer.parseInt(num2);
   answer = CalculatorMethods.add(aa,bb);
}else{
     //converts the String into Double
}

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.