njellis
IS-IT--Management
- Jul 17, 2006
- 29
I have tried and tried different things, but my if compare statement isn't working. I want to take the second character entered, then via an IF statement say "If Operator = "X" then set caseSelect to "1""
But it keeps jumping to the else. When I run it, it shows that it is a correct string but... just aint working.
Thanks!
/**
*
*/
/**
* @author njellis
*
*/
import java.util.Scanner;
import java.lang.Character;
public class MathProgram {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter a simple equation or type 'done' when finished");
Boolean done = false;
while (!false) {
Scanner userInputScanner = new Scanner(System.in);
String userInput = userInputScanner.nextLine();
userInput = userInput.toLowerCase();
if (userInput.contains("done")) {
done = true;
}
else {
char a = userInput.charAt(0);
int numFirst = Character.getNumericValue(a);
char b = userInput.charAt(2);
int numSecond = Character.getNumericValue(b);
int caseSelect = 0;
//Convert operator to string then compare and set caseSelect
// String OperatorFirst = Character.toString(userInput.charAt(1));
char c = userInput.charAt(1);
String Operator = Character.toString(c);
System.out.println("Test " + Operator); //Test shows correct operator but next part is not working properly...
// cannot figure out why if statement isn't working correctly.
//Compare string of Operator variable with possible matching values.
if (Operator == "x") {
caseSelect = 1;
}
else if (Operator == "X") {
caseSelect = 1;
}
else if (Operator == "*") {
caseSelect = 1;
}
else if (Operator == "+") {
caseSelect = 2;
}
else if (Operator == "-") {
caseSelect = 3;
}
else if (Operator == "/") {
caseSelect = 4;
}
else {
System.out.println("hmm... the operator seems incorrecet. Try any of these */+-");
caseSelect = 5;
}
System.out.println("Case Select Set to " + caseSelect);
switch (caseSelect) {
case 1:
System.out.println("The result is: " + (numFirst*numSecond));
break;
case 2:
System.out.println("The result is: " + (numFirst+numSecond));
break;
case 3:
System.out.println("The result is: " + (numFirst-numSecond));
break;
case 4:
System.out.println("The result is: " + (numFirst/numSecond));
break;
}
}
}
}
}
But it keeps jumping to the else. When I run it, it shows that it is a correct string but... just aint working.
Thanks!
/**
*
*/
/**
* @author njellis
*
*/
import java.util.Scanner;
import java.lang.Character;
public class MathProgram {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter a simple equation or type 'done' when finished");
Boolean done = false;
while (!false) {
Scanner userInputScanner = new Scanner(System.in);
String userInput = userInputScanner.nextLine();
userInput = userInput.toLowerCase();
if (userInput.contains("done")) {
done = true;
}
else {
char a = userInput.charAt(0);
int numFirst = Character.getNumericValue(a);
char b = userInput.charAt(2);
int numSecond = Character.getNumericValue(b);
int caseSelect = 0;
//Convert operator to string then compare and set caseSelect
// String OperatorFirst = Character.toString(userInput.charAt(1));
char c = userInput.charAt(1);
String Operator = Character.toString(c);
System.out.println("Test " + Operator); //Test shows correct operator but next part is not working properly...
// cannot figure out why if statement isn't working correctly.
//Compare string of Operator variable with possible matching values.
if (Operator == "x") {
caseSelect = 1;
}
else if (Operator == "X") {
caseSelect = 1;
}
else if (Operator == "*") {
caseSelect = 1;
}
else if (Operator == "+") {
caseSelect = 2;
}
else if (Operator == "-") {
caseSelect = 3;
}
else if (Operator == "/") {
caseSelect = 4;
}
else {
System.out.println("hmm... the operator seems incorrecet. Try any of these */+-");
caseSelect = 5;
}
System.out.println("Case Select Set to " + caseSelect);
switch (caseSelect) {
case 1:
System.out.println("The result is: " + (numFirst*numSecond));
break;
case 2:
System.out.println("The result is: " + (numFirst+numSecond));
break;
case 3:
System.out.println("The result is: " + (numFirst-numSecond));
break;
case 4:
System.out.println("The result is: " + (numFirst/numSecond));
break;
}
}
}
}
}