Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote this code to convert Roman to Integer But this code is throwing  index out of bound exception.


What I have tried:

<pre>package practice_projects;

import java.util.*;

public class roman {

	public static void main(String[] args) {
		
		int result = 0;
			Scanner scan = new Scanner(System.in);
        
			
        
        HashMap<string,integer>roman = new HashMap<string,integer>();
        
        roman.put("I",1);
         roman.put("V",5);
         roman.put("X",10);
         roman.put("L",50);
         roman.put("C",100);
         roman.put("D",500);
         roman.put("M",1000);
        
         String input = scan.next();
         String input_final = input.toUpperCase();
         	
      
         
         for(int i=0;i<=input_final.length();i++) {
              
        	 if(input_final.charAt(i)>input_final.charAt(i-1)) {
        		 		result += roman.get(input_final.charAt(i-1))-roman.get(input_final.charAt(i));
        	 }else {
        		 result += roman.get(input_final.charAt(i));
        	 }
        	 
        	System.out.println();
        
        	
         }
        
		
	}

}
Posted
Updated 9-May-22 1:44am
Comments
Graeme_Grant 9-May-22 6:45am    
It's telling you that your index is outside the bounds of the collection
0x01AA 9-May-22 6:46am    
This
for(int i=0;i<=input_final.length();i++) should be
for(int i=0;i<input_final.length();i++)

In Java - as in most languages, an array or other collection is accessed via indexes which run from 0 to the number of items in the collection minus one.
So an array with 3 elements, can have valid indexes of 0, 1, and 2 only - all other values are outside the bounds of the collection because they refer to elements that don't exist.
So when your loop looks like this and input_final had three elements:
Java
for(int i=0;i<=input_final.length();i++) {

The values of i would be 0, 1, 2, and 3, which throws an exception because the final index is not in the collection.
Change the test to exclude the equality check and it only uses 0, 1, and 2 so no problem occurs:
Java
for(int i=0;i<input_final.length();i++) {
 
Share this answer
 
Comments
CPallini 9-May-22 7:58am    
5.
Unfortunately, while correct, the Griff's suggestion doesn't completely fix your code (for instance, when i=0, your code tries to access input_final.charAt(-1); there are other issues however...). Try
Java
public static void main(String[] args)
{
  int result = 0;
  Scanner scan = new Scanner(System.in);

  HashMap< Character, Integer> roman = new HashMap< Character, Integer>();

  roman.put('I',1);
  roman.put('V',5);
  roman.put('X',10);
  roman.put('L',50);
  roman.put('C',100);
  roman.put('D',500);
  roman.put('M',1000);

  String input = scan.next();
  String input_final = input.toUpperCase();

  int next = 0;
  for(int i=0;i<input_final.length()-1;i++)
  {
    int cur =  roman.get(input_final.charAt(i));
    next =  roman.get(input_final.charAt(i+1));
    if ( cur >= next )
      result += cur;
    else
      result -= cur;
  }
  result += next;

  System.out.println(result);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900