Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Really need help..

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Really need help..

    Can anyone explain why this code isn't working.. It's supposed to take a year and convert it to roman numerals.. I can't use returns.. "not allowed"..
    import java.util.Scanner;
    public class threedottwentysix {
    	public static void main(String[] args){
    	Scanner in = new Scanner(System.in);
    	System.out.println("Please Enter a year that is less than 3999: ");
    	String input = in.nextLine();
    	int digit = input.length();
    	String digitOne = "";
    	String digitTwo = "";
    	String digitThree = "";
    	String digitFour = "";
    	if (digit == 4){
    			int num1 = Integer.parseInt(input.substring(0, 1));
    			switch(num1){
    			case 1: digitOne = "M"; break;
    			case 2: digitOne = "MM"; break;
    			case 3: digitOne = "MMM"; break;
                                  }
                            }
    		else if (digit == 3)
                            { 
    			int num2 = Integer.parseInt(input.substring(1,2));
    			switch(num2)
                                {
    			case 1: digitTwo = "C"; break;
    			case 2: digitTwo = "CC"; break;
    			case 3: digitTwo = "CCC"; break;
    			case 4: digitTwo = "CD"; break;
    			case 5: digitTwo = "D"; break;
    			case 6: digitTwo = "DC"; break;
    			case 7: digitTwo = "DCC"; break;
    			case 8: digitTwo = "DCCC"; break;
    			case 9: digitTwo = "CM"; break;
                                }
                           }
    		else if (digit == 2)
                           {
    			int num3 = Integer.parseInt(input.substring(2,3));
    			switch(num3)
                                 {
    			case 1: digitThree = "X"; break;
    			case 2: digitThree = "XX"; break;
    			case 3: digitThree = "XXX"; break;
    			case 4: digitThree = "XL"; break;
    			case 5: digitThree = "L"; break;
    			case 6: digitThree = "LX"; break;
    			case 7: digitThree = "LXX"; break;
    			case 8: digitThree = "LXXX"; break;
    			case 9: digitThree = "XC"; break;
                               }
                            }
    		else if (digit == 1)
                               {
    			int num1 = Integer.parseInt(input.substring(3,4));
    			switch(num1)
                             {
    			case 1: digitFour = "I"; break;
    			case 2: digitFour = "II"; break;
    			case 3: digitFour = "III"; break;
    			case 4: digitFour = "IV"; break;
    			case 5: digitFour = "V"; break;
    			case 6: digitFour = "VI"; break;
    			case 7: digitFour = "VII"; break;
    			case 8: digitFour = "VIII"; break;
    			case 9: digitFour = "IX"; break;
                                }
                           }
    	if (digit == 3)
                      {
    			int num2 = Integer.parseInt(input.substring(1,2));
    			switch(num2) {
    			case 1: digitTwo = "C"; break;
    			case 2: digitTwo = "CC"; break;
    			case 3: digitTwo = "CCC"; break;
    			case 4: digitTwo = "CD"; break;
    			case 5: digitTwo = "D"; break;
    			case 6: digitTwo = "DC"; break;
    			case 7: digitTwo = "DCC"; break;
    			case 8: digitTwo = "DCCC"; break;
    			case 9: digitTwo = "CM"; break;
                                  }
                            }
    		else if (digit == 2)
                            {
    			int num3 = Integer.parseInt(input.substring(2,3));
    			switch(num3)
                                  {
    			case 1: digitThree = "X"; break;
    			case 2: digitThree = "XX"; break;
    			case 3: digitThree = "XXX"; break;
    			case 4: digitThree = "XL"; break;
    			case 5: digitThree = "L"; break;
    			case 6: digitThree = "LX"; break;
    			case 7: digitThree = "LXX"; break;
    			case 8: digitThree = "LXXX"; break;
    			case 9: digitThree = "XC"; break;
                                 }
                             }
    		else if (digit == 1)
                             {
    			int num4 = Integer.parseInt(input.substring(3,4));
    			switch(num4)
                                 {
    			case 1: digitFour = "I"; break;
    			case 2: digitFour = "II"; break;
    			case 3: digitFour = "III"; break;
    			case 4: digitFour = "IV"; break;
    			case 5: digitFour = "V"; break;
    			case 6: digitFour = "VI"; break;
    			case 7: digitFour = "VII"; break;
    			case 8: digitFour = "VIII"; break;
    			case 9: digitFour = "IX"; break;
                                   }
                             }
    	if (digit == 2)
                    {
    			int num3 = Integer.parseInt(input.substring(2,3));
    			switch(num3)
                                    {
    			case 1: digitThree = "X"; break;
    			case 2: digitThree = "XX"; break;
    			case 3: digitThree = "XXX"; break;
    			case 4: digitThree = "XL"; break;
    			case 5: digitThree = "L"; break;
    			case 6: digitThree = "LX"; break;
    			case 7: digitThree = "LXX"; break;
    			case 8: digitThree = "LXXX"; break;
    			case 9: digitThree = "XC"; break;
                                  }
                            }
    		else if (digit == 1) 
                            {
    			int num4 = Integer.parseInt(input.substring(3,4));
    			switch(num4)
                                  {
    			case 1: digitFour = "I"; break;
    			case 2: digitFour = "II"; break;
    			case 3: digitFour = "III"; break;
    			case 4: digitFour = "IV"; break;
    			case 5: digitFour = "V"; break;
    			case 6: digitFour = "VI"; break;
    			case 7: digitFour = "VII"; break;
    			case 8: digitFour = "VIII"; break;
    			case 9: digitFour = "IX"; break;
                                 }
                            }
    	if (digit == 1)
                            {
    			int num4 = Integer.parseInt(input.substring(3,4));
    			switch(num4)
                                   {
    			case 1: digitFour = "I"; break;
    			case 2: digitFour = "II"; break;
    			case 3: digitFour = "III"; break;
    			case 4: digitFour = "IV"; break;
    			case 5: digitFour = "V"; break;
    			case 6: digitFour = "VI"; break;
    			case 7: digitFour = "VII"; break;
    			case 8: digitFour = "VIII"; break;
    			case 9: digitFour = "IX"; break;
                                   }
                            }
    	System.out.print(digitOne + digitTwo + digitThree + digitFour);
    	}	
    }

    output:
    Please Enter a year that is less than 3999:
    1962
    M

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Really need help..

    code isn't working
    Can you explain what "isn't working" means?
    If there are errors, copy the full text of the error messages and paste it here.
    If the output is wrong, post the contents of the console from when the code is executed and explain what is wrong with it.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    One problem I see with being able to understand the posted code is its formatting.
    I see lots of hidden }} at the ends of statements making it hard to see the structure of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Really need help..

    Instead of using all of those else if statements, could you try using nested switch statements?

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really need help..

    See, my prof. taught us nested if statements and told me to do it that way.. I honestly don't know the correct format in nested switch statements.. I have a Java book but really isn't helpful for more complicated problems.. well.. complicated being relative..

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Really need help..

    You need to do more work on the program's design before continuing.
    How should a 4 digit number be converted? Look at doing it digit by digit.
    After doing the first digit, how should the program handle the next digit and the next.
    Now what if there are only 3 digits? How can some of the code used for the 4 digit number be used for the 3 digit number?
    And the same for the 2 digit number and the one digit number.

    Look at how to convert each of the following. Use a variable for getting the digits, not hardcoded values like in the posted code.
    1111
    111
    11
    1


    BTW The formatting of the code still has too many hidden }s at the end of statements.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really need help..

    When you say the "hardcoded values," do you mean the int num1 = Integer.parseInt(input.substring(1,2);? Isn't num1 considered a variable?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Really need help..

    int num1 = Integer.parseInt(input.substring(1,2);
    The 1 and 2 are hardcoded values. They should be variables.
    If you don't understand my answer, don't ignore it, ask a question.