# validate a mathmatical operator

Page 1 of 1

## 2 Replies - 241 Views - Last Post: 21 February 2011 - 07:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=217778&amp;s=24fbe6b4db32dd29a8a8b5ef2ba33b84&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 eric4215

Reputation: 0
• Posts: 51
• Joined: 09-September 10

# validate a mathmatical operator

Posted 21 February 2011 - 06:11 PM

I am making a command line calculator in Java, I have figured out most of the project, the calculating essentials, but I need help with the part where the user inputs a string variable which is the mathematic sign, I cannot figure out how to validate that it is only a “-“,”+”,”/” or “*” sign, I have seen examples on validating an integer but can’t see how it would apply to an operator, Here’s the code I have so far starting from the last mathematical operation, I’m sure there a better way to do it.
```  else {
if (operator.equals("/"))

System.out.printf("The quotient of " + A + " and " + B + "is %20.2f%n", answer);
{
if (B == 0)
System.out.println("You cannot devide by zero!!!");
}
System.out.print(" Do you wish to continue ? 1 = Yes; 0 = No: ");
Continue = input.nextInt();
System.out.println();
}
{
while (!operator.equals("+")&&
!operator.equals("-")&&
!operator.equals("*")&&
!operator.equals("/"));
System.out.println("You must choose a correct operator!!!");
}
System.out.print(" Do you wish to continue ? 1 = Yes; 0 = No: ");
Continue = input.nextInt();
System.out.println();

```

Is This A Good Question/Topic? 0

## Replies To: validate a mathmatical operator

### #2 eric4215

Reputation: 0
• Posts: 51
• Joined: 09-September 10

## Re: validate a mathmatical operator

Posted 21 February 2011 - 07:15 PM

I am also trying it this way, but I run into a problem with the string operator, since it will not let me use a string with a switch, so I changed it to char, but I still get errors mainly with the "enter the operator" statement, do i need to convert anything, which way would work better, and what am I doing wrong. Any help would be appreciated. Thanks.

```
char operator;
boolean valid = true;

Scanner input = new Scanner(System.in);

int Continue = 1;
while(Continue == 1)

{

System.out.print(" Welcome to the JAVA Calculator!!!");
System.out.println();

System.out.print(" Enter the first number : ");
double A = input.nextDouble();

System.out.print(" Enter the second number : ");
double B = input.nextDouble();

System.out.print(" Enter the operator symbol : ");
operator = input.next();

switch (operator)
{
case '+':
break;

case '-':
break;

case '*':
break;

case '/':
break;

default:
valid = false;
break;
}

if (valid == true)
else
System.out.println("You must choose a correct operator!!!");

}

```

### #3 Dogstopper

Reputation: 2907
• Posts: 11,148
• Joined: 15-July 08

## Re: validate a mathmatical operator

Posted 21 February 2011 - 07:21 PM

The issue is that you can't switch on a String...only char. Use if statements instead or convert it to char (using charAt)