0

Just a quick question, can i use this to include "left" "right" back" and "forward" all together or do i have to do them separately?

An error came up, so if anyone knows how to include them all together then please help. Thanks

Scanner console = new Scanner(System.in);
for (int i = 0; i < 100; i++) {
    System.out.println("Please type in either the word \"left\" or \"right\" or \"back\" or \"foward\": ");
    String s = console.next();

    if (s.equalsIgnoreCase("left")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
    } if (s.equalsIgnoreCase("right")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
    } if (s.equalsIgnoreCase("back")) {
        myFinch.setWheelVelocities(-100,-100,S);
    } if (s.equalsIgnoreCase("foward")) {
        myFinch.setWheelVelocities(130,130,S);
    } else if (s.equalsIgnoreCase != ("left" && "right" && "back" && "foward")) {
        myFinch.quit();
    }
Zong
  • 6,160
  • 5
  • 32
  • 46
  • *an error came up*? Wonderful! So you just need to solve that error you didn't show us ..... – Ingo May 01 '14 at 17:19
  • the error was the last else if statement, i just wanted to know how i can incorporate them all together easily without the error or do i have to do them separately – user3357247 May 01 '14 at 17:27
  • @user3357247 I know my answer is long but I reformatted it so that the answer to your direct question comes first, the rest is just some suggestions and my explanation for those suggestions. – Farmer Joe May 02 '14 at 22:28

3 Answers3

2

I would go with a switch statement :

switch (s.toLowerCase()) {
    case "left":
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
        break;
    case "right":
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
        break;
    }
Zoyd
  • 3,449
  • 1
  • 18
  • 27
  • 1
    No, it's not since he doesnt show the default statement and thishow it's not the same as the if statement. the default should be used (see my answer) to get all cases which are not left, right, forward, back. In this case myFinch.quit(). AND do nor forget to mention that he requires JDK 7 or above to make sure the switch can handle strings. – Emanuel May 01 '14 at 17:58
  • Indeed. Your answer, however, uses apostrophes instead of quotation marks, which is why it doesn't even compile, and you forgot to convert the string to lowercase. – Zoyd May 01 '14 at 18:59
  • Thanks for the hint. Fixed it. – Emanuel May 01 '14 at 20:02
1

First off to answer your question, in Java you should use String.equals to compare strings, or String.equalsIgnoreCase. This is because this example will fail:

String a = "a";
if (a == "a") {
    // Will not be true because you are comparing the reference to the string "a" 
} else if (a.equals("a")) {
    // Will work because you are comparing on the value of the two strings
}

ref: == vs .equals

I noticed you did this in the first few statements, but on the last statement, the one in question, you did not.

While the statement you were trying to form was not necessary I find it would be useful to share the correct way to do it:

    // OMITTED CODE
    } else if (s.equalsIgnoreCase("left") && s.equalsIgnoreCase("right") && s.equalsIgnoreCase("back") && s.equalsIgnoreCase("foward") ) {
        myFinch.quit();
    }

You must make each boolean statement complete, in the sense that it must evaulate to a boolean.

s.equalsIgnoreCase != x// this is simply  method so it could not be compared to anything using the != operator
("left" && "right" /* etc */ ) // "left", "right" are not booleans but simply strings.

Java is a very explicit language so shortcuts as the one you attempted are often far and few between.

Secondly you should use the format:

if (/* condition 1*/) {
    // code if condition 1 is true
} else if (/* condtion 2 */) {
    // code if condition 2 is true but condition 1 is false
} else {
    // code if condition 1 and condition 2 are false
}

The else if statement is used to simplify code that would take the following format:

if (/* condition */) {
    // code will run if condtion is true
} else {
    if (/* sub-condition */) {
        // code will run if sub-condition is true, but condition is false
    } else {
        if (/* sub-sub-condition */) {
            // code will run if sub-sub-condition is true, but sub-condition and condition are false
        } else {
            // code will run if condition, sub-sub-condition, and sub-condition is false
        }
    }
}

To avoid long chains of such code:

if (/* condition */) {
    // code will run if condtion is true
} else { if (/* sub-condition */) {
    // code will run if sub-condition is true, but condition is false
} else { if (/* sub-sub-condition */) {
    // code will run if sub-sub-condition is true, but sub-condition and condition are false
} else {
    // code will run if condition, sub-sub-condition, and sub-condition is false
}}}

The formatting can be seen clearly from here to the current setup:

if (/* condition */) {
    // code will run if condtion is true
} else if (/* sub-condition */) {
    // code will run if sub-condition is true, but condition is false
} else if (/* sub-sub-condition */) {
    // code will run if sub-sub-condition is true, but sub-condition and condition are false
} else {
    // code will run if condition, sub-sub-condition, and sub-condition is false
}

These statements were created to read in a logical way:

If the first condtion is met follow the first set of instructions,
else if the first condition wasnt met then try the second condition and instructions,
else if the first two conditions failed try the third set!,
else Damn! Just resort to these instructions

Imagine a scenario where you are taking care of your friend's cat. You are unable to talk about how to care for the cat before your friend leaves but they left you a set of instructions:

Dear friend,

    Thank you for looking after muffins.  She is a very high maintenance cat.
 She has four kinds of food and depending on her mood you should feed her one of
 these four:  "Purina Super Awesome Cat Time", "Cat Feast 2000", "Cat Chow", and 
 "Canned".

 If you come over and she is waiting at the door give her the "Cat Fest 2000",
 If she is not waiting at the door, but instead attacks your leg as you enter the
 house you should give her the "Cat Chow",
 If she is not at the door, and didn't attack you but is instead wearing a small hat
 you should give her the "Purina Super Awesome Cat Time" and play a game of Bridge with
 her.
 If none of those things happened then give her the "Canned".

 Thanks!  See you Caturday!

Instead of sending yourself on this monstrous task, with clearly outlined danger, perhaps we want to write a very intelligent robot to go in and take care of the cat each day.

 //  Upon arrival
 if ( Cat.isWaitingAtTheDoor() ) {
     Cat.feed("Cat Fest 2000");
 } else if ( Cat.didAttackWhenYouWalkedIn() ) {
     Cat.feed("Cat Chow");
 } else if ( Cat.isWearingSmallHat() ) {
     Cat.feed("Purina Super Awesome Cat Time");
     Cat.playBridgeWith(self);
 } else {
     Cat.feed("Canned");
 }

So reformat your code to match that structure and you will find you don't need that last condition:

Scanner console = new Scanner(System.in);
for (int i = 0; i < 100; i++) {
    System.out.println("Please type in either the word \"left\" or \"right\" or \"back\" or \"foward\": ");
    String s = console.next();

    if (s.equalsIgnoreCase("left")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
    } else if (s.equalsIgnoreCase("right")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
    } else if (s.equalsIgnoreCase("back")) {
        myFinch.setWheelVelocities(-100,-100,S);
    } else if (s.equalsIgnoreCase("foward")) {
        myFinch.setWheelVelocities(130,130,S);
    } else {
        myFinch.quit();
    }
}

The way you had it set up initially you essentially are not creating a branch structure.

consider this:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
} if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

This will out put:

i = 0
i = 1

Not what we intended!

This is because the above code is equivalent to:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
}
// Two separate statements altogether


if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

Whereas:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
} else if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

Will give:

i = 0

What we wanted, now it is a branched statement, it checks the first if statement then all else if statements following and lastly if none were true resorts the else statement. This seems to be your intention since there is no space for variab;e reassignment between these if statements.

Community
  • 1
  • 1
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
0

Since JDK 7 you can use strings in switches.

Means:

switch(s.toLowerCase()) {
case "left":
    myFinch.setWheelVelocities(90,90,S);
    myFinch.setWheelVelocities(0,100,S);
break;
case "right":
    myFinch.setWheelVelocities(90,90,S);
    myFinch.setWheelVelocities(100,0,S);
break;
case 'back':
    myFinch.setWheelVelocities(-100,-100,S);
break;
case "foward"
    myFinch.setWheelVelocities(130,130,S);
break;
/** .. and other cases **/
default:
    myFinch.quit();
}
Emanuel
  • 8,027
  • 2
  • 37
  • 56