Reputation: 23
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
Upvotes: 1
Views: 1195
Reputation: 89
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;
}
}
Upvotes: 1
Reputation: 1666
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);
Upvotes: 1
Reputation: 3871
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
Upvotes: 1
Reputation: 1413
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.
Upvotes: 0