Reputation: 1
I think I have the proper code in order for the conversions to take place (thank you to user Matt Bryant for helping) but I'm confused now on how to proceed. What I've learned so far how to type code into the main body (public static void main(String args[])) but this code that I have gotten assistance with doesn't seem to go in the main method. Can anyone please help me get this code running? :) (Also I have an error saying my scanner cannot resolve to a type/variable). Im aware nothing is writting in the main method but that's only because I am unsure of what to do at this point.
import java.util.Scanner;
public class romannumeralconversion {
public static void main(String args[]) {
}
public String ToRoman() {
Scanner myKeyboard = new Scanner(System.in);
System.out.println("Enter the integer: ");
number = myKeyboard.nextInt();
myKeyboard.close();
return ToRoman(number);
}
public String ToRoman(int number) {
if ((number < 1 || (number > 3999)))
return "INVALID";
if (number >= 1000)
return "M" + ToRoman(number - 1000);
if (number >= 900)
return "CM" + ToRoman(number - 900);
if (number >= 500)
return "D" + ToRoman(number - 500);
if (number >= 400)
return "CD" + ToRoman(number - 400);
if (number >= 100)
return "C" + ToRoman(number - 100);
if (number >= 90)
return "XC" + ToRoman(number - 90);
if (number >= 50)
return "L" + ToRoman(number - 50);
if (number >= 40)
return "XL" + ToRoman(number - 40);
if (number >= 10)
return "X" + ToRoman(number - 10);
if (number >= 9)
return "IX" + ToRoman(number - 9);
if (number >= 5)
return "V" + ToRoman(number - 5);
if (number >= 4)
return "IV" + ToRoman(number - 4);
if (number >= 1)
return "I" + ToRoman(number - 1);
return "INVALID";
}
}
Upvotes: 0
Views: 210
Reputation: 10959
To make it run in your main class you need to instantiate it and then call the methods you want to run, so you will probably want something like this in your main
romannumeralconversion rnc = new romannumeralconversion();
System.out.println(rnc.ToRoman());
Also your number
variable is not assigned a type, as you are reading in the next int, put int
in front like this int number = myKeybaord.nextInt()
You will also need to put the following import statement at the very top of your class file to import the Scanner
class (Just realised you do have this)
import java.util.Scanner;
Note that convention is that classes start with a Capital letter and methods should be named with the first letter of each internal word capitalized. See java Naming Convensions.
Upvotes: 1
Reputation: 1123
You can put the code for ToRoman() in your main block and call ToRoman(int number) from there:
public static void main(String args[]) {
Scanner myKeyboard = new Scanner (System.in);
System.out.println("Enter the integer: ");
int number = myKeyboard.nextInt();
System.out.println(ToRoman(number));
myKeyboard.close();
}
Another issue is, ToRoman(int number), will always print out an "INVALID" at the end of the output. So you might stop that behavior, by adding another base case condition in ToRoman(int number) like:
if(number == 0) return "";
By the way, in order to be able to call ToRoman(int number), you need to either define it as a static method, or create an object of the class and then call the method.
Upvotes: 1