Author |
Message |
uberwalla
|
Posted: Fri Feb 22, 2008 9:38 pm Post subject: Roman Numeral Converter Problem |
|
|
Hey there thanks to those that have helped me through some of this with commands I didn't know. Trying to move into java from extensive javaSCRIPT is hard. A lot of different forms.
Anyways this is what i have so far:
Java: |
import java.util.Scanner;
public class Assignment05 {
public static void main (String [] args ) {
String [] ones = new String [10];
ones [0] = "";
ones [1] = "I";
ones [2] = "II";
ones [3] = "III";
ones [4] = "IV";
ones [5] = "V";
ones [6] = "VI";
ones [7] = "VII";
ones [8] = "VIII";
ones [9] = "IX";
String [] tens = new String [10];
tens [0] = "";
tens [1] = "X";
tens [2] = "XX";
tens [3] = "XXX";
tens [4] = "XL";
tens [5] = "L";
tens [6] = "LX";
tens [7] = "LXX";
tens [8] = "LXXX";
tens [9] = "XC";
String [] hundreds = new String [10];
hundreds [0] = "";
hundreds [1] = "C";
hundreds [2] = "CC";
hundreds [3] = "CCC";
hundreds [4] = "CD";
hundreds [5] = "D";
hundreds [6] = "DC";
hundreds [7] = "DCC";
hundreds [8] = "DCCC";
hundreds [9] = "CM";
String [] thousands = new String [4];
thousands [0] = "";
thousands [1] = "M";
thousands [2] = "MM";
thousands [3] = "MMM";
String [][] all = new String [4][];
all [0] = ones;
all [1] = tens;
all [2] = hundreds;
all [3] = thousands;
String roman = "";
Scanner input = new Scanner (System. in);
System. out. print("Enter a year, and I will convert it to roman numerals: ");
int year = input. nextInt();
String yearStr = Integer. toString(year );
int length = yearStr. length();
for (int i = 0; i < length; i++ ) {
roman += all [length - i - 1][yearStr. charAt(i )];
}
System. out. format(roman );
}
}
|
The code compiles fine but when I run it and enter a number i get the error:
any idea what i can do to fix this? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Fri Feb 22, 2008 9:44 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
code: | roman += all[length - i - 1][yearStr.charAt(i)]; | yearStr.charAt(i) returns a char. '3' == 51. You need use Integer.toString() or something like yearStr.charAt(i) - '0'. |
|
|
|
|
|
uberwalla
|
Posted: Fri Feb 22, 2008 9:52 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
ok must ask how do i use the Integer.toString to my charAt? i tried using it like:
yearStr.charAt(Integer.toString(i))
but i got an error.
i am quite lost |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Fri Feb 22, 2008 10:09 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
Try Integer.toString(yearStr.substring(i, i + 1)). |
|
|
|
|
|
uberwalla
|
Posted: Fri Feb 22, 2008 10:16 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
i have no idea what this means: (its what i get using substring)
Java: |
Assignment05. java: 70: cannot find symbol
symbol : method toString (java. lang. String)
location: class java. lang. Integer
roman += all [length - i - 1][Integer. toString(yearStr. substring(i, i + 1))];
^
1 error
|
|
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Fri Feb 22, 2008 10:21 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
ugh, my fault: Integer.parseInt instead. |
|
|
|
|
|
uberwalla
|
Posted: Fri Feb 22, 2008 10:27 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
thanks a lot man. i actually found another way around this.
Java: |
import java.util.Scanner;
public class Assignment05 {
public static void main (String [] args ) {
String [] ones = new String [10];
ones [0] = "";
ones [1] = "I";
ones [2] = "II";
ones [3] = "III";
ones [4] = "IV";
ones [5] = "V";
ones [6] = "VI";
ones [7] = "VII";
ones [8] = "VIII";
ones [9] = "IX";
String [] tens = new String [10];
tens [0] = "";
tens [1] = "X";
tens [2] = "XX";
tens [3] = "XXX";
tens [4] = "XL";
tens [5] = "L";
tens [6] = "LX";
tens [7] = "LXX";
tens [8] = "LXXX";
tens [9] = "XC";
String [] hundreds = new String [10];
hundreds [0] = "";
hundreds [1] = "C";
hundreds [2] = "CC";
hundreds [3] = "CCC";
hundreds [4] = "CD";
hundreds [5] = "D";
hundreds [6] = "DC";
hundreds [7] = "DCC";
hundreds [8] = "DCCC";
hundreds [9] = "CM";
String [] thousands = new String [4];
thousands [0] = "";
thousands [1] = "M";
thousands [2] = "MM";
thousands [3] = "MMM";
String [][] all = new String [4][];
all [0] = ones;
all [1] = tens;
all [2] = hundreds;
all [3] = thousands;
String roman = "";
Scanner input = new Scanner (System. in);
System. out. print("Enter a year, and I will convert it to roman numerals: ");
int year = input. nextInt();
String yearStr = Integer. toString(year );
int length = yearStr. length();
for (int i = 0; i < length; i++ ) {
roman += all [length - i - 1][Integer. valueOf(yearStr. substring(i,i+ 1))];
}
System. out. print(roman + "\n");
}
}
|
thanks for all your help |
|
|
|
|
|
richcash
|
Posted: Sat Feb 23, 2008 11:42 pm Post subject: Re: Roman Numeral Converter Problem |
|
|
If you have time/interest, try to shorten your program and significantly reduce the memory your program uses. You do not need to store all of those values in those arrays. Use some recursion (and math). And you also don't need to do the costly conversion of integer to string. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
uberwalla
|
Posted: Sun Feb 24, 2008 11:49 am Post subject: Re: Roman Numeral Converter Problem |
|
|
well i am still relatively new to java so i do not know how to easily reduce the program. i mostly know some of the commands from c/c++ and javascript, because they are relatively close. but java is still weird to me in how you have to format the code and such. |
|
|
|
|
|
|