Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

If Statement not working...

If Statement not working...

If Statement not working...

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.



 * @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));
            case 2:
                System.out.println("The result is: " + (numFirst+numSecond));
            case 3:
                System.out.println("The result is: " + (numFirst-numSecond));
            case 4:
                System.out.println("The result is: " + (numFirst/numSecond));



RE: If Statement not working...


== is suitable for comparing primitives. Use the equals() method when comparing Strings :

CODE --> Java ( fragment )

            if (Operator.equals("x")) {
                caseSelect = 1;

Next time please post your code between [code] and [/code] TGML tags.


RE: If Statement not working...

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

Thanks for your help that solved the problem.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close