-3

How might I make an if statement with multiple conditions? I thought I would use ||, but when I use this, it says "The || operator is undefined for the argument type(s) boolean, java.lang.String"

System.out.print("Which pit would you like to select? ");
String temp = input.nextLine();
{
    if(temp == "A" || "a")
        pit = 13;
    else if(temp = "B" || "b")
        pit = 12;
    else if(temp = "C" || "c")
        pit = 11;
    else if(temp = "D" || "d")
        pit = 10;
    else if(temp = "E" || "e")
        pit = 9;
    else if(temp = "F" || "f")
        pit = 12;
    else
        System.out.println("Not a valid pit!");
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Jon Harlow
  • 9
  • 1
  • 1
  • 3
  • 3
    A coupe of things to point out here. A single = operator is assignment. Don't do that in a condition. Also == should not be used to compare strings. You should use "A".equals(temp); – bhspencer Feb 03 '15 at 16:02
  • 1
    If you're sing Java 7, use a `switch(temp.toLowerCase()) { case "a": ... case "b": ... }`. You could also store the base data in a `Map map` and then do `Integer pit = map.get(temp.toLowerCase());` – Luiggi Mendoza Feb 03 '15 at 16:05

6 Answers6

4

First of all, use .equals() to compare strings.

Second of all, this is the way you use the || operator:

if(temp.equals("A") || temp.equals("a"))
    pit = 13;

An even better approach is :

if("A".equalsIgnoreCase(temp))
    pit = 13;

This way :

  1. You will never get a NullPointerException
  2. You do not have to use the || operator

Finally, as a note, bear in mind that the equality operator in Java is ==, whereas = is the assignment operator.

parakmiakos
  • 2,994
  • 9
  • 29
  • 43
3

In java when using group condition operator, you must re-specify the value you are testing each time...

if(temp == "A" || "a")

Would become

if(temp.equals("A") || temp.equals("a"))

Notice that I compared using the equals function as you are comparing Object and == will only compare the memory addres value.

Also since the multiple condition check the same letter with different case, you can use

if ("a".equals(temp.toLowerCase())

as @LuiggiMendoza stated.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
2

You want to use the equals method to compare strings

So your else if statement should look like this

else if(temp.equals("D") || temp.equals("d"))

steven35
  • 3,747
  • 3
  • 34
  • 48
1
if(temp == "A" || temp == "a")
      pit = 13;

A == B evaluates to a boolean (A == B) || C would therefore evaluate too boolean || String. Since String is not boolean, you get that error.

Note that since you're comparing Strings and not primitives you should use equals:

if(temp.equals("A") || temp.equals("a"))
      pit = 13; 
runDOSrun
  • 10,359
  • 7
  • 47
  • 57
0

On Java 7 and above, better use a switch case with strings as

switch (temp.toLowerCase()) {
case "a":
    pit = 13;
    break;
case "b":
    pit = 12;
    break;
case "c":
    pit = 11;
    break;
case "d":
    pit = 10;
    break;
case "e":
    pit = 9;
    break;
case "f":
    pit = 12;
    break;
default:
    System.out.println("Not a valid pit!");
}

It becomes much more readable this way.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

Change your original code to the following:

System.out.print("Which pit would you like to select? ");
String temp = input.nextLine();
{
    if(temp == "A" || temp == "a")
        pit = 13;
    else if(temp = "B" || temp == "b")
        pit = 12;
    else if(temp = "C" || temp == "c")
        pit = 11;
    else if(temp = "D" || temp == "d")
        pit = 10;
    else if(temp = "E" || temp == "e")
        pit = 9;
    else if(temp = "F" || temp == "f")
        pit = 12;
    else
        System.out.println("Not a valid pit!");

You have to specify which variable you are testing each time. What if you are testing this, for example:

String temp = "A";
String pmet = "B";

if(temp.equals("A") || pmet.equals("B")) {
    //Code to run here...
}

In the previous code, you are saying "If temp is "A" OR pmet is "B", then run the section in the if statement". You can test more than just one variable using the || operator. Also, as previously stated, when you are testing string values, it is best to use ".equals(STRING)". For example, if(temp.equals("A")) would be a better way to code this. You will get the correct result, but it is not really the most appropriate way to code this.

Ryan Shukis
  • 345
  • 1
  • 10