Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

If Statement not working...

njellis (IS/IT--Management) (OP)
26 Mar 12 4:24
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;
                
            }
            
            
            }

            
        }
        

    }
        
}
 
feherke (Programmer)
26 Mar 12 4:38
Hi

== 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.

Feherke.
http://feherke.github.com/

njellis (IS/IT--Management) (OP)
26 Mar 12 11:25
I will put it in the code blocks in the future.

Thanks for your help that solved the problem.
 

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!

Back To Forum

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