This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Read in char and use as int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read in char and use as int" Watch "Read in char and use as int" New topic
Author

Read in char and use as int

Trevor Whitaker
Greenhorn

Joined: Oct 03, 2012
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

Integer.parseInt() or Scanner.nextInt() will convert the String "3" to the int value 3.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
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

Joined: Oct 03, 2012
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

Joined: Jan 03, 2004
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

Joined: Mar 17, 2011
Posts: 7677
    
  19

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()?).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read in char and use as int