Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Statement not working...

Status
Not open for further replies.

njellis

IS-IT--Management
Jul 17, 2006
29
0
0
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;

}


}


}


}

}
 
Hi

[tt]==[/tt] is suitable for comparing primitives. Use the [tt]equals()[/tt] method when comparing [tt]String[/tt]s :
Code:
            [b]if[/b] [teal]([/teal]Operator[highlight][teal].[/teal][COLOR=darkgoldenrod]equals[/color][teal]([/teal][/highlight][green][i]"x"[/i][/green][highlight][teal])[/teal][/highlight][teal])[/teal] [teal]{[/teal]
                caseSelect [teal]=[/teal] [purple]1[/purple][teal];[/teal]
            [teal]}[/teal]

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
I will put it in the code blocks in the future.

Thanks for your help that solved the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top