Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2014
    Posts
    28
    Rep Power
    0

    Question Allowing the User to input a number and assign this to an 'int' variable

    Hi!
    I need the user to be able to input a number, and for the program to assign this value to an 'int' variable.

    I know how to do this with a 'string' variable:
    Java Code:
    String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have? \n" +
    			"(NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)");
    But I need to know how to do this with an 'int' variable. Thanks for your help!

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Allowing the User to input a number and assign this to an 'int' variable

    You can parse the String to an int using the Integer.parseInt(String) method of the Integer class:

    So, first as a string, then to an int.

Similar Threads

  1. Have user input custom variable name
    By kkid in forum New To Java
    Replies: 7
    Last Post: 11-13-2012, 11:41 PM
  2. Counting number of user input
    By cs3 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 09:33 AM
  3. Random Number Generator with User Input
    By person287 in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 10:49 AM
  4. Creating a number of objects based on user input.
    By Kevinius in forum New To Java
    Replies: 21
    Last Post: 04-03-2011, 08:53 AM
  5. geting user input (number)
    By kliane in forum New To Java
    Replies: 9
    Last Post: 01-17-2010, 12:25 PM

Tags for this Thread

Posting Permissions

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