Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Input a word, return a number

    How does it work with Strings? For example, enter words like "one", "two", or "three" and returns integers 1, 2, or 3.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default Re: Input a word, return a number

    With java version 1.7 you could use a switch statement.
    With Older versions could use a Map or a chain of if/else if statements
    Or an array with a loop
    or some other method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Input a word, return a number

    I know the switch case for int -> words, but how do you do it the other way around?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default Re: Input a word, return a number

    With version 1.7:
    case "one":
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Input a word, return a number

    case "one": return 1;

    Is it?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default Re: Input a word, return a number

    What follows the case has not changed. The programmer can put anything there.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Input a word, return a number

    So I can put

    case "January": return 1;

    ?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default Re: Input a word, return a number

    If you have the 1.7 JDK, try it and see what happens.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java word scramble -Trouble reading input
    By dru36 in forum New To Java
    Replies: 15
    Last Post: 11-26-2011, 07:09 PM
  2. Any Other Methods for Number to Word Conversion?
    By arnelcolar in forum New To Java
    Replies: 14
    Last Post: 04-14-2011, 04:45 PM
  3. Replies: 13
    Last Post: 10-31-2010, 08:42 PM
  4. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 04:56 AM
  5. number to word with decimals
    By fertas in forum New To Java
    Replies: 3
    Last Post: 08-30-2008, 03:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •