Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read in char and use as int

 
Trevor Whitaker
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt() or Scanner.nextInt() will convert the String "3" to the int value 3.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic