I am trying to create a program that reads in a single digit number followed by a roman numeral and multiply them together. I read them into chars but is it possible to use the char instead of the ASCII value. For example, if i entered 3V, it would do 3*5 and not 51*5? I know I can achieve this using a switch statement but is there an easier way? Thanks!!
Jeff Verdegan wrote:Integer.parseInt() or Scanner.nextInt() will convert the String "3" to the int value 3.
How would you implement it in this program?
Well, you've already got a String containing your '3' character plus some other stuff. You might want to look at the methods in the String class to see what it provides that helps you get specific pieces of it.
Trevor Whitaker wrote:I read them into chars but is it possible to use the char instead of the ASCII value. For example, if i entered 3V, it would do 3*5 and not 51*5? I know I can achieve this using a switch statement but is there an easier way?
Yes. You've already worked out that if you put '3' into an int, you'll get 51, so what do you think you'll get if you put '4' in instead?...or '7', or '1'?
Now, given what you know, what do you think you might have to do to convert them to their "digit" values?
BTW, I haven't seen a crn() (convert to roman numerals) function for years(*), and certainly never in hybrid form.
(*) Although, of course, yours is the reverse (rcn()?).
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here