• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Read in char and use as int

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.parseInt() or Scanner.nextInt() will convert the String "3" to the int value 3.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?).
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic