1 Replies - 1787 Views - Last Post: 05 February 2012 - 08:54 PM Rate Topic: -----

#1 monkeyboi101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 12

Convert year to Roman numerals w/ switch statement

Posted 05 February 2012 - 06:51 PM

Hey guys. I'm having trouble converting a year to roman numerals using a switch statement in java. There are no errors, but when I run the program, I can input the year, but it doesnt output anything. Any help would be appreciated. Thanks!

import java.util.Scanner;

public class YearToRomanNumerals
{  

	public static void main(String[] args)
   {  
	
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Please enter your year to be converted into Roman Numerals:");
		char digit = keyboard.nextchar();
		System.out.println("");
	
		YearToRomanNumerals yearToRomanNumerals= new YearToRomanNumerals();
      String year;

   }

   private void convertYear(String year)
   {  String romanNumeral;

      romanNumeral= getRomanNumeral(year);
      System.out.println("Year "+year+ " equals "+romanNumeral+" in Roman Numeral Format");
   }

   private String getRomanNumeral(String year)
   {  String ones,
             tens,
             hundreds,
             thousands,
             romanNumeral;

      ones= getOnes(year);
      tens= getTens(year);
      hundreds= getHundreds(year);
      thousands= getThousands(year);

      romanNumeral= thousands+hundreds+tens+ones;

      return romanNumeral;
   }

   private String getOnes(String year)
   {  char digit;
      String romanNumeral= "";

      digit= year.charAt(3);

      switch(digit)
      {  case '0':
           break;
         case '1':
           romanNumeral= "I";
           break;
         case '2':
           romanNumeral= "II";
           break;
         case '3':
           romanNumeral= "III";
           break;
         case '4':
           romanNumeral= "IV";
           break;
         case '5':
           romanNumeral= "V";
           break;
         case '6':
           romanNumeral= "VI";
           break;
         case '7':
           romanNumeral= "VII";
           break;
         case '8':
           romanNumeral= "VIII";
           break;
         case '9':
           romanNumeral= "IX";
           break;
         default:
           romanNumeral= "ERROR";
           break;
      }

      return romanNumeral;
   }

   private String getTens(String year)
   {  char digit;
      String romanNumeral= "";

      digit= year.charAt(2);

      switch(digit)
      {  case '0':
           break;
         case '1':
           romanNumeral= "X";
           break;
         case '2':
           romanNumeral= "XX";
           break;
         case '3':
           romanNumeral= "XXX";
           break;
         case '4':
           romanNumeral= "XL";
           break;
         case '5':
           romanNumeral= "L";
           break;
         case '6':
           romanNumeral= "LX";
           break;
         case '7':
           romanNumeral= "LXX";
           break;
         case '8':
           romanNumeral= "LXXX";
           break;
         case '9':
           romanNumeral= "XC";
           break;
         default:
           romanNumeral= "ERROR";
           break;
      }

      return romanNumeral;
   }

   private String getHundreds(String year)
   {  char digit;
      String romanNumeral= "";

      digit= year.charAt(1);

      switch(digit)
      {  case '1':
           romanNumeral= "C";
           break;
         case '2':
           romanNumeral= "CC";
           break;
         case '3':
           romanNumeral= "CCC";
           break;
         case '4':
           romanNumeral= "CD";
           break;
         case '5':
           romanNumeral= "D";
           break;
         case '6':
           romanNumeral= "DC";
           break;
         case '7':
           romanNumeral= "DCC";
           break;
         case '8':
           romanNumeral= "DCCC";
           break;
         case '9':
           romanNumeral= "CM";
           break;
         default:
           romanNumeral= "ERROR";
           break;
      }

      return romanNumeral;
   }

   private String getThousands(String year)
   {  char digit;
      String romanNumeral= "";

      digit= year.charAt(0);

      switch(digit)
      {  case '1':
           romanNumeral= "M";
           break;
         case '2':
           romanNumeral= "MM";
           break;
         case '3':
           romanNumeral= "MMM";
           break;
         case '4':
           romanNumeral= "MMMM";
           break;
         default:
           romanNumeral= "ERROR"+year.substring(1)+" is ";
           break;
      }

      return romanNumeral;
   }
} 


This post has been edited by blackcompe: 05 February 2012 - 08:31 PM
Reason for edit:: Please use [code] tags when posting to the forum.


Is This A Good Question/Topic? 0
  • +

Replies To: Convert year to Roman numerals w/ switch statement

#2 blackcompe  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Convert year to Roman numerals w/ switch statement

Posted 05 February 2012 - 08:54 PM

Before I even address other methods in your code, I'll start at the beginning. Why are you retrieving a character from the user? You have keyboard.nextchar(). That doesn't compile. Anyway, you should be retrieving a String if that's what convertYear accepts.

Scanner kbd = new Scanner(System.in);
System.out.println("<your prompt>");
String year = kbd.nextLine(); //you could kbd.next() too
new YearToRomanNumerals().convertYear(year);


This post has been edited by blackcompe: 05 February 2012 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1