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