0

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";
    }
}
Smit
  • 4,685
  • 1
  • 24
  • 28
user2840309
  • 1
  • 1
  • 1
  • Your method String toRoman needs to return a string but you are returning an int value – Rohan Oct 02 '13 at 22:50
  • Isn't the int value number while the string is ToRoman(number);? and under the scanner code i have written return ToRoman(number); – user2840309 Oct 02 '13 at 23:04

2 Answers2

1

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.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • Thanks I did as you said to put the assign int to number, however I still get an error message saying Scanner cannot be resolved to a type. – user2840309 Oct 02 '13 at 23:09
  • Excellent thank you so much. One last thing, The program is running now however whenever I enter an integer it will give me the correct answer followed by "invalid". For example: I enter the integer 3 and it outputs IIIINVALID.Do I need a certain else if statement for this? – user2840309 Oct 02 '13 at 23:17
  • Actaully I didn't realize but user Niki answered this question! – user2840309 Oct 02 '13 at 23:20
1

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.

Niki
  • 1,105
  • 1
  • 10
  • 17