简体   繁体   中英

Converting a letter to a number using a switch statement within a method - Java

I am trying to convert a roman numeral entered by the user into the correct value it represents.

My code:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
    String romanNumeral = in.nextLine();
    romanNumeralToInt(romanNumeral);
    System.out.println(romanNumeral);

}

public static int romanNumeralToInt(String romanNumeral) {
    switch (romanNumeral) {
    case "I":
        romanNumeral = 1;
        break;
    case "V":
        romanNumeral = 5;
        break;
    case "X":
        romanNumeral = 10;
        break;
    case "L":
        romanNumeral = 50;
        break;
    case "C":
        romanNumeral = 100;
        break;
    case "D":
        romanNumeral = 500;
        break;
    case "M":
        romanNumeral = 1000;
        break;
    }
}

I have tried numerous different ways of converting this. I tried to use char instead of string but the same problem came up.

I have also tried to use the line:

public static int romanNumeralToInt(String romanNumeral, int romanDecimal) {

and then using:

    case "I":
        romanDecimal = 1;
        break;

Which didn't work. I have tried printing out just System.out.println(romanNumeral) which also didn't work. I am kinda stuck and don't really understand how I go about returning a value as an integer when it was inputted as a string in a method

Welcome to stackoverflow! My first thought is that you're not using your return value from your function, to do this, add a return statement instead of a break statement. it should read

 case "I":
        return 1; 

then return from it:

System.out.println(romanNumeralToInt("I"));

Or , simply make sure youre assigning strings instead of integers inside your function :

 case "I":
        romanNumeral = "1";
        break;

but to accomplish this, you'll need to change the scope of romanNumeral to global if im not mistaken and this wouldnt be the best practice, in my opinion

The current code you have pasted here does not seem right. No return written for the method, even though it clearly has an int return type. Was it compiled correctly?

Anyhow, change your method to have a return;

public static int romanNumeralToInt(String romanNumeral) {
    int intNumeral = 0;
    switch (romanNumeral) {
    case "I":
        intNumeral = 1;
        break;
    case "V":
        intNumeral = 5;
        break;
    case "X":
        intNumeral = 10;
        break;
    case "L":
        intNumeral = 50;
        break;
    case "C":
        intNumeral = 100;
        break;
    case "D":
        intNumeral = 500;
        break;
    case "M":
        intNumeral = 1000;
        break;
    }
    return intNumeral;
}

Then in the main method let the methods return variable be stored in an int type variable;

int intNumericConverted = romanNumeralToInt(romanNumeral);

You don't need to convert it you can simply return integer values like this:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
    String romanNumeral = in.nextLine();
    romanNumeralToInt(romanNumeral);
    System.out.println(romanNumeral);

}

public static int romanNumeralToInt(String romanNumeral) {

    switch (romanNumeral) {
    case "I":
        return 1;
    case "V":
        return 5;
    case "X":
        return 10;
    case "L":
        return 50;
    case "C":
        return 100;
    case "D":
        return 500;
    case "M":
        return 1000;
    }
}

Integer class has static method toString() - you can use it:

int i = 1234;
String str = Integer.toString(i);

Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.

Short answer: Learn Java and stop lying when you ask questions.

Some explanation: Learn Java; you are attempting to change the value of a method parameter, which can will never work in Java. The value of a parameter after calling a method is guaranteed (by the language) to be the same as the value of the parameter before calling a method.

Stop lying; the code you show as the mothod body will not compile. The static method declares a return value but has no return statement.

The solution to your problem.

  1. Declare a return value in the romanNumeralToInt method.
  2. Set the value of the return value inside the switch statement.
  3. Return the return value from the romanNumeralToInt method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM