NEPat10
NEPat10

Reputation: 159

Why do I get this "unreachable statement" error?

I am converting a roman numeral input to it's integer value. In my convertChar method, I keep getting an error that it is an unreachable statement whenever I add a break statement in to the code. I don't know why this is. I'm a new student and I must have done something wrong and I was hoping maybe someone can show me what I did wrong and how to fix it. It must have something to do with the way I set the methods up right? I still get confused on what variables to input so maybe I messed up there but I'm not experienced enough to know exactly what I did wrong. Here is everything I have:

public class RomanNumeralConverter {

public int romanInput() {       
    return convert(getUserInput());
}   

public String getUserInput() {

    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral in uppercase: ");
    String userInput = numberInput.next();
    numberInput.close();
    return userInput;
}

public int convert (String userInput) {
    int result = 0;
    int subtractamount = 0;
    int x = userInput.length();
    while(x != 0) {
    char romanConvert = userInput.charAt(x);
        if(x >= 1) {
        if(convertChar(romanConvert) >= convertChar(userInput.charAt(x - 1))) {
        subtractamount += convertChar(userInput.charAt(x - 1));
    }
}
    result += convertChar(romanConvert);
    x--;

}
result -= subtractamount;
return result;

}

public static char convertChar(char value) {
    switch (value) {
        case 'I':
            return 1;
            break;
        case 'V':
            return 5;
            break;
        case 'X':
            return 10;
            break;
        case 'L':
            return 50;
            break;
        case 'C':
            return 100;
            break;
        case 'D':
            return 500;
            break;
        case 'M':
            return 1000;
            break;
        default:
            System.out.println("Invalid character!");
            return 0;
            break;
}
    return value;

}

public void printValue() {
    System.out.println(romanInput());
}

public static void main(String[] args) {
    new RomanNumeralConverter().printValue();
}
}

Upvotes: 0

Views: 117

Answers (3)

Makoto
Makoto

Reputation: 106508

The problem is in your switch statement.

A default case can be thought of like the else in an if-else statement; it will always execute if no other condition in the switch is satisfied. If you are performing a return (or throw) inside of a default case, any code that follows after that will not be reachable.

You have two options:

  • Change the return statements to only assign a value to result instead, meaning that there's only one point of return from your code, or
  • Remove the return result from after your switch.

Upvotes: 1

ucsunil
ucsunil

Reputation: 7504

In Java, it is a compile error to have statements that will never be reached while execution. In your case, the break statement will never be reached as there is a return statement above it. Also that last return statement will never be reached as you already would have returned in any case by the end of the switch block.

Upvotes: 2

austin wernli
austin wernli

Reputation: 1801

Your problem lies in your switch statement. You can minimize this occurring elsewhere by attempting to have methods return only once (which i think is best practice)

public static char convertChar(char value) {
    char result;
    switch (value) {
        case 'I':
            result = 1;
            break;
        case 'V':
            result = 5;
            break;
        case 'X':
            result = = 10;
            break;
        case 'L':
            result = 50;
            break;
        case 'C':
            result = 100;
            break;
        case 'D':
            result = 500;
            break;
        case 'M':
            result = 1000;
            break;
        default:
            System.out.println("Invalid character!");
            result = 0;
            break;
                   }
        return result
}

Upvotes: 2

Related Questions