I'm writing a program that converts roman numerals into a decimal number. The program ask the user to input a roman numeral and then ask if it wants to print it as a roman or decimal number. I've written this program in C++ and it ran perfectly. Now I have to translate it to java. The only program I am having is how to use pass by reference in java. Here is my code:
main.java
romanType.java
in the method convertToDecimal, roman not set to anything so when I try to output the roman numeral as a decimal, it comes out to 0. What is the problem?
main.java
package romannumeralconverter;
import java.util.*;
import java.util.Scanner;
public class driver {
public static void main(String[] args) {
String roman = "";
romanType newRoman = new romanType();
newRoman.enterRoman();
newRoman.storeRoman(roman);
newRoman.convertToDecimal(roman);
System.out.println("Which would you like to print: ");
System.out.println("1. Roman");
System.out.println("2. Decimal");
int choice;
Scanner scan = new Scanner(System.in);
choice = scan.nextInt();
if(choice == 1)
newRoman.printRoman();
else
newRoman.printDecimal();
}
}
romanType.java
package romannumeralconverter;
import java.util.*;
public class romanType {
String roman;
int integer;
public void enterRoman(){
System.out.println("Enter Roman Number: ");
}
public void storeRoman(String roman){
Scanner input = new Scanner(System.in);
roman = input.next();
}
public void convertToDecimal(String roman){
int integerTotal = 0;
int[] number = new int[10];
int i = 0;
while(i < roman.length()){
switch(roman.length()){
case 'M':
number[i] = 1000;
integerTotal += 1000;
break;
case 'D':
number[i] = 500;
integerTotal += 500;
break;
case 'C':
number[i] = 100;
integerTotal += 100;
break;
case 'L':
number[i] = 50;
integerTotal += 50;
break;
case 'X':
number[i] = 10;
integerTotal += 10;
break;
case 'V':
number[i] = 5;
integerTotal += 5;
break;
case 'I':
number[i] = 1;
integerTotal += 1;
break;
default:
break;
}
for(int a = 0; a < roman.length()-1; a++){
if(number[a] < number[a+1]){
integerTotal -= (number[a]*2);
}
}
integer = integerTotal;
}
}
public void printRoman(){
System.out.println(roman);
}
public void printDecimal(){
System.out.println(integer);
}
}
in the method convertToDecimal, roman not set to anything so when I try to output the roman numeral as a decimal, it comes out to 0. What is the problem?