Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Read in char and use as int

Trevor Whitaker
Greenhorn
Posts: 3
Greetings,

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
Bartender
Posts: 6109
6
Integer.parseInt() or Scanner.nextInt() will convert the String "3" to the int value 3.

Praveen Kumar M K
Ranch Hand
Posts: 256
So what you essentially need is a way to convert a character 3 to an integer 3. How about looking into some in-built functions for String to Integer conversion?

Trevor Whitaker
Greenhorn
Posts: 3
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?

Jeff Verdegan
Bartender
Posts: 6109
6
Trevor Whitaker wrote:
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.

Winston Gutkowski
Bartender
Posts: 10417
63
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.

Winston

(*) Although, of course, yours is the reverse (rcn()?).