CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    Question Names with Roman Numerals

    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:
    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;
    	}
    
    }
    my main methods i have this:

    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 my problem is definitely in the for loop where i split the names.
    I tried to print out the first and last part but got these errors

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at RoyalNamesMethod.getLast(RoyalNamesMethod.java:14)
    	at RoyalNames.main(RoyalNames.java:26)
    tried changing the limits to -1 and -2 but could not get them to print.

    Does anyone catch something I am doing wrong or recommend a different way to do this?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Names with Roman Numerals

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at RoyalNamesMethod.getLast(RoyalNamesMethod.java:14)
    The code at line 14 attempted to use an index of 1 on an array with less than 2 items in it.
    What was in the array? Print it out to see:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    Norm

  3. #3
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    Re: Names with Roman Numerals

    Quote Originally Posted by Norm View Post
    The code at line 14 attempted to use an index of 1 on an array with less than 2 items in it.
    What was in the array? Print it out to see:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    I tried printing it out by adding the line of code you provided in my methods

    Code:
    public String getLast(String input){
    		String[] parts = input.split(" ");
    		String last = parts[1];
    		System.out.println("an ID "+ java.util.Arrays.toString(parts));
    		return last;
    	}
    and added this to my main method
    Code:
    rnm.getLast(names[0]);
    then i get this:

    How many names will you input?
    3
    Enter the names:
    Bill IX
    Bill IV
    Bill L
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at RoyalNamesMethod.getLast(RoyalNamesMethod.java:14)
    at RoyalNames.main(RoyalNames.java:20)

    I cant get it to print out. Am I trying to get it print wrong? or did i split the string incorrectly?

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Names with Roman Numerals

    The print statement needs to be executed BEFORE the statement where the exception happens. Otherwise it will never be executed.
    Norm

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: Names with Roman Numerals

    Quote Originally Posted by rcheeks23 View Post
    I had an interview question
    In that job interview they probably were looking at the following to asses your maturity as programmer:

    - Are you aware of the fact that "alphabetic order" generally is more involved than just the "natural order" of the String type?
    - Are you aware of the possibility of using a Comparator when sorting? What design pattern would that be?
    - Could you think of a more general and denser way of evaluating the Roman numbers?

    In today's competitive job market it's seldom enough to know Java only at the very basic level. I suggest you take a look at a Java certification test. You don't necessarily have to sit it (even though it's probably an asset if you lack a degree). It will still help you get a feel of what's expected from you.
    Last edited by wolle; March 18th, 2018 at 04:55 AM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured