Rainier
Rainier

Reputation: 11

Why isn't my Java switch statement working?

I am trying to do the following for a Java switch method with a series of JUnit Asserts but am stuck on using "less than" and "greater than" for two cases (see string/int error below), and am not sure how to use the ">" and "<" in my cases.

Here is the exercise followed by my code, followed by the error.

/*  
Create a method which uses a switch statement to return a String representing the int passed in as a parameter to the method:
            • given 1 return "One"
            • given 2 return "Two"
            • given 3 return "Three"
            • given 4 return "Four"
            • given an integer > 4, return "Too big"
            • given an integer < 1, return "Too small"
*/
@Test
public void switchIntExample() {
    assertEquals("One", stringRepInt(1));
    assertEquals("Two", stringRepInt(2));
    assertEquals("Three", stringRepInt(3));
    assertEquals("Four", stringRepInt(4));
    assertEquals("Too big.", stringRepInt(>4));      
//        assertEquals("Too small.", stringRepInt(<4));
}

//Switch statement for above:
public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;           
     //TODO:  question on how to do LESS THAN and GREATER THAN:
     // error line: 
     case  (numVar > 4):
            numVar = "Too big.";
            break;

        default:
            break;
    }
    System.out.println(numVar);
    return numVar;
}

ERROR: Error:(293, 27) java: bad operand types for binary operator '>' first type: java.lang.String second type: int

Upvotes: 0

Views: 3161

Answers (4)

Bimde
Bimde

Reputation: 742

The purpose of the default section is to deal with all cases not dealt with by the switch cases. You should be taking advantage of that (as @GreenMatt and @FredK mentioned in comments) by putting the 'if checks' in the default section, as follows:

public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;
        default:
            if(numberSize > 4)
                numVar = "Too big";
            break;
    }
    System.out.println(numVar);
    return numVar;
}

Further, you could add else if (numberSize < 1) numVar = "Too small"; under the if statement if you want to check for number's smallest than one. This is also important because it prevents your method from returning null. (Which currently happens if the user enters a value less than 1)

The resulting code is as follows:

public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;
        default:
            if(numberSize > 4)
                numVar = "Too big";
            else
                numVar = "Too small";
            break;
    }
    System.out.println(numVar);
    return numVar;
}

Upvotes: 0

Pieter van der Heijden
Pieter van der Heijden

Reputation: 190

You should add it to the default section, so like this:

default:
    numVar = "Too Big";
    break;

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201517

case statements only support constant expressions (you cannot do less than, or greater than, in a case and you can't test numVar - the String - with less than). You can use an if and something like

public String stringRepInt(int numberSize) {
    String numVar = null;
    if (numberSize > 4) {
         numVar = "Too big.";
    } else {
        switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;       
        default:
            break;
    }
    System.out.println(numVar);
    return numVar;
}

Upvotes: 1

Sudha
Sudha

Reputation: 220

In the error line you are comparing String and int.

error line: case (numVar > 4): numVar = "Too big."; break;

you have declared numVar as String and comparing that with int value 4. Please correct that or I think you are trying to compare numberSize with value 4. Convert that String to int and compare it.

Upvotes: -1

Related Questions