Results 1 to 2 of 2
  1. #1
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default int and String, help me

    Hi, I'm making this class to take a customer's order for a project, but I hit a snag. Basically I'm asking a customer for the amount of pizzas they want, then the size, the first topping, and the last topping.
    The amount of pizzas they want will initiate the amount of times my "for loop" will run to ask the same round of questions.

    for the size of the pizzas, I've assigned (1)small (2)medium (3)large. The customer enters the respective number, and i take the input as

    Java Code:
    int size=JOptionpane.showInputDialog("Whats the size?(1)small(2)medium(3)large" )
    I'll get a int number between 1-3, but now how do i assign a string value to an integer?

    like how do i get the input of "1" to mean "small"?

    Thanks

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    Step 1 is quite straight forward. You can use the parseInt function on the Integer class:
    Java Code:
    int number = Integer.parseInt(someString);
    Remember to check for NumberFormatExceptions if the user types in rubbish.
    The next bit of making the 1 mean small can be as simple as defining a constant in your class but you may want to investigate using an enum instead.

    Hope this helps.

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  2. String vs new String
    By bugger in forum New To Java
    Replies: 20
    Last Post: 11-26-2007, 12:21 PM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 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
  •