Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Convert the number to Roman numerals

    Hi

    I have been asked to write code that converts the number to Roman numerals.

    Fortunately, I wrote the code and it works properly.

    My question is

    In standard Roman numerals, no more than three consecutive copies of the same letter are used.

    I have tried many times to discover what the proper statement which I must write in my code is that voids using three consecutive copies of the same letter

    this is the code

    Java Code:
    import java.util.Scanner;
    
    public class RomanNumeral {
    	public static void main(String[] args){
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Please enter a number between 1 and 3999: ");
    		int number = scan.nextInt();
    		
    		String roman="";
    		
    		
    		while(number<=0 || number>3999){
    			System.out.println("Invalid input.  You must enter a number between 1 and 3999");
    			System.out.print("Please enter another number now: ");
    			number = scan.nextInt();
    			}
    		
    		while(number>=1000){
    			roman += "M";
    			number-=1000;
    		}
    		
    		while(number>=900){
    			roman += "CM";
    			number-=900;
    		}
    		
    		while(number>=500){
    			roman += "D";
    			number-=500;
    		}
    		
    		while(number>=400){
    			roman += "CD";
    			number-=400;
    		}
    		
    		while(number>=100){
    			roman += "C";
    			number-=100;
    		}
    		
    		while(number>=90){
    			roman += "XC";
    			number-=90;
    		}
    		
    		while(number>=50){
    			roman += "L";
    			number-=50;
    		}
    		
    		while(number>=40){
    			roman += "XL";
    			number-=40;
    		}
    		
    		while(number>=10){
    			roman += "X";
    			number-=10;
    		}
    		
    		while(number>=9){
    			roman += "IX";
    			number-=9;
    		}
    		
    		while(number>=5){
    			roman += "V";
    			number-=5;
    		}
    		
    		while(number>=4){
    			roman += "IV";
    			number-=4;
    		}
    		
    		while(number>=1){
    			roman += "I";
    			number-=1;
    		}
    		
    		System.out.println(roman);
    	}
    }
    I hope you can help me

    Thanks

    :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Hi
    Fortunately, I wrote the code and it works properly.

    I have tried many times to discover what the proper statement which I must write in my code is that voids using three consecutive copies of the same letter
    It's OK for 3 consecutive letters to occur, just not 4. Your code seems to me to work just fine, so I'm not sure what your question is.

    Oh, and thanks for starting a new thread! :)

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    removed......
    Last edited by al_Marshy_1981; 02-21-2010 at 11:18 PM. Reason: brain is turning to mush

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    If the number entered was 1111, then the roman String would be: "IIII" which is wrong. I think that is what he means.
    Huh? If I run this code and enter "1111" I get MCXI back which is the correct answer. Am I missing something here?

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    haha yes you are right I thought number was being reduced to zero for some reason in each while loop. Time for bed I think :D

  6. #6
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Thank u 4 response

    I have another problem

    The output should be the same format

    Java Code:
    Please enter a number between 1 and 3999: 1073
    0 in Roman numerals is MLXXIII
    Instead of 0 , the programe must gives the same number that the user has entered

    I've tried to fix this problem but I couldn't

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏƒ Ңόρę View Post
    Thank u 4 response
    You're welcome, but what the heck happened to your first problem? Have you decided to just ignore it? We need closure here.

    edit: hm, I guess you've been abducted by aliens...
    Last edited by Fubarable; 02-23-2010 at 05:05 AM.

  8. #8
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Hi Fubarable

    Thank you for your passing and responsing

    About the first problem, I read your response and I saw there is no problem

    I just was confused by this rule

    In standard Roman numerals, no more than three consecutive copies of the same letter are used.

    Also the second problem, I fixed yesterday

    This is code after fixing

    Java Code:
    import java.util.Scanner;
    
    public class RomanNumeral {
    	public static void main(String[] args){
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Please enter a number between 1 and 3999: ");
    		int number = scan.nextInt();
    		[COLOR="Red"]int number1 = number;[/COLOR] // I wrote this variable to hold the number that the user enters
    		
    		String roman="";
    		
    		
    		while(number<=0 || number>3999){
    			System.out.println("Invalid input.  You must enter a number between 1 and 3999");
    			System.out.print("Please enter another number now: ");
    			number = scan.nextInt();
    			[COLOR="Red"]number1=number;[/COLOR]
    			}
    		
    		while(number>=1000){
    			roman += "M";
    			number-=1000;
    		}
    		
    		while(number>=900){
    			roman += "CM";
    			number-=900;
    		}
    		
    		while(number>=500){
    			roman += "D";
    			number-=500;
    		}
    		
    		while(number>=400){
    			roman += "CD";
    			number-=400;
    		}
    		
    		while(number>=100){
    			roman += "C";
    			number-=100;
    		}
    		
    		while(number>=90){
    			roman += "XC";
    			number-=90;
    		}
    		
    		while(number>=50){
    			roman += "L";
    			number-=50;
    		}
    		
    		while(number>=40){
    			roman += "XL";
    			number-=40;
    		}
    		
    		while(number>=10){
    			roman += "X";
    			number-=10;
    		}
    		
    		while(number>=9){
    			roman += "IX";
    			number-=9;
    		}
    		
    		while(number>=5){
    			roman += "V";
    			number-=5;
    		}
    		
    		while(number>=4){
    			roman += "IV";
    			number-=4;
    		}
    		
    		while(number>=1){
    			roman += "I";
    			number-=1;
    		}
    		
    		System.out.println(number1 + " in Roman numerals is " + roman);
    	}
    }
    If there is another way to solve that please tell me.

    Thanks

  9. #9
    Rocknsock is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Re: Convert the number to Roman numerals

    import java.util.Scanner;


    public class Teste {


    public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    System.out.println("Type a number");
    int num = -1;
    String roman = "";
    do{
    if(sc.hasNextInt())
    num = sc.nextInt();
    else{
    System.out.println("Error");
    sc.nextLine();
    sc.next();
    }

    }while(num<=0 && num>3999);


    while(num>=900){
    if(num==900){
    roman+="CM";
    num=0;
    }
    else{
    roman+="M";
    num-=1000;
    }
    }
    while(num>=400){
    if(num==400){
    roman+="CD";
    num=0;
    }
    else{
    roman+="D";
    num-=500;
    }
    }
    while(num>=90){
    if(num==90){
    roman+="XC";
    num=0;
    }
    else{
    roman+="C";
    num-=100;
    }
    }
    while(num>=40){
    if(num==40){
    roman+="XL";
    num=0;
    }
    else{
    roman+="L";
    num-=50;
    }
    }
    while(num>=9){
    if(num==9){
    roman+="IX";
    num=0;
    }
    else{
    roman+="X";
    num-=10;
    }
    }
    while(num>=4){
    if(num==4){
    roman+="IIIV";
    num=0;
    }
    else{
    roman+="V";
    num-=5;
    }
    }
    while(num>=1){
    roman+="I";
    num-=1;
    }
    System.out.println("Msg " +roman);



    }

    }

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Convert the number to Roman numerals

    @Rocknsock, any reason you decided to resurrect a thread that is almost 3 years old to spoonfeed code? Recommended reading:
    The Problem with Spoon-feeding

Similar Threads

  1. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 10:06 PM
  2. convert from roman numerals to integers and vice versa
    By number1cynic in forum New To Java
    Replies: 10
    Last Post: 01-18-2010, 11:54 PM
  3. convert number to string
    By wizard_oz in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 07:04 PM
  4. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  5. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •