I made a hashmap that stores roman numerals as keys and their decimal numbers as values. The error says "incompatible types - found java.lang.Object but expected int". I'm just trying to get the value of the roman numeral when I write "conversions.get(numOne.charAt(x));" What am I doing wrong here?
import java.util.Scanner;
import java.util.HashMap;
public class test
{
static Scanner sc = new Scanner(System.in);
static HashMap conversions = new HashMap();
public static void main(String args[]){
conversions.put('I',1);
conversions.put('V',5);
conversions.put('X',10);
conversions.put('L',50);
conversions.put('C',100);
conversions.put('D',500);
conversions.put('M',1000);
String numOne = "XIX";
for(int x = 0; x <= numOne.length()-2; x++){
int temp1 = conversions.get(numOne.charAt(x));
int temp2 = conversions.get(numOne.charAt(x+1));
}
}
}
Change the line:
static HashMap conversions = new HashMap();
to
static Map<Character,Integer> conversions = new HashMap<Character,Integer>();
or as of Java 7, we can avoid some duplication by doing the following
static Map<Character,Integer> conversions = new HashMap<>();
All in all, this will autobox your primitives and resolve your problem