I had an interview question a couple months ago and one of the questions was to accept a name with a roman numeral up to the number 50.
1)you ask the user how many names they are going to put in.
2)sort them out first by alphabetical order
3) if the first names are equal to each other sort them out by the roman numeral
EX:
input
3
Bill IV
Bill L
Bill IX
Bill V
Bill X
output:
Bill IV
Bill V
Bill IX
Bill X
Bill L
for my methods i have this:
my main methods i have this:Code:public class RoyalNamesMethod { //gets the first part of the name of the string and stores is at index 0 of the parts array public String getFirst(String input){ String[] parts = input.split(" "); String firstName = parts[0]; return firstName; } //gets the roman numeral of the string and stores it in index 1 of the parts array public String getLast(String input){ String[] parts = input.split(" "); String last = parts[1]; return last; } //converts the roman numeral to an integer but first checks for the specific rules for a number public int convertLast(String romanNum){ int value = 0; romanNum.toUpperCase(); for(int i=0; i < romanNum.length()-1; i++){ if(romanNum.charAt(i) == 'I' && romanNum.charAt(i+1) == 'V'){ value += 4; i++; } else if(romanNum.charAt(i) == 'I' && romanNum.charAt(i+1) == 'X'){ value += 9; i++; } else if(romanNum.charAt(i) == 'X' && romanNum.charAt(i+1) == 'L'){ value += 40; i++; } else{ switch(romanNum.charAt(i)){ case 'I': value += 1; break; case 'V': value += 5; break; case 'X': value += 10; break; case 'L': value += 50; break; } } } return value; } }
i my problem is definitely in the for loop where i split the names.Code:import java.util.Arrays; import java.util.Scanner; public class RoyalNames { public static void main(String[] args) { Scanner scan = new Scanner(System.in); RoyalNamesMethod rnm = new RoyalNamesMethod(); System.out.println("How many names will you input?"); int amount = scan.nextInt(); String[] names = new String[amount+1]; System.out.println("Enter the names: "); for(int i=0; i<names.length; i++) names[i]=scan.nextLine(); Arrays.sort(names); //for(int i =0; i<names.length; i++) //System.out.println(names[i]); //int[] last = new int[names.length]; for(int i=0; i<names.length; i++){ String temp1 = rnm.getFirst(names[i]); //temporarily store the first name String temp2 = rnm.getFirst(names[i+1]); //temporarily stores the first name of the next string String temp3 = rnm.getLast(names[i]).toUpperCase(); //stores the roman numeral String temp4 = rnm.getLast(names[i+1]).toUpperCase(); //stores the roman numeral of the next string int romanNum1 = rnm.convertLast(temp3); //converts the roman numeral to an int int romanNum2 = rnm.convertLast(temp4); //converts the next roman numberal to an int //last[i] = romanNum1; //System.out.println(last[i]); if(temp1.equalsIgnoreCase(temp2)){ //if the first name of the two stored are equal to each other if(romanNum2 > romanNum1){ //and if the second roman numeral is greater then the first one String temp5 = names[i]; //then names will switch positions names[i] = names[i+1]; names[i+1] = temp5; } } } for(int i =0; i<names.length; i++) System.out.println(names[i]); //print out the names in order } }
I tried to print out the first and last part but got these errors
tried changing the limits to -1 and -2 but could not get them to print.Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at RoyalNamesMethod.getLast(RoyalNamesMethod.java:14) at RoyalNames.main(RoyalNames.java:26)
Does anyone catch something I am doing wrong or recommend a different way to do this?




Reply With Quote
