Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Allowing user to input variable.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Allowing user to input variable.

    Hello, I'm new to programming, and could use a little help with something!

    I feel like I'm missing the obvious answer, but I just cant think of it!

    I'm trying to allow the user to input 5 variables when the program prompts them, and then these variables will be used later on as the number amount of fabricated items that the user has to use.

    For Example:
     


    "Hello! Input the number of Arrows you want!"

    "=user inputs #="

    "You have # arrows! How many arrows would you like to use?"

    "user inputs #"



    I need the number the user inputted to be a variable, so that the user cant use more of the item than they inputed that they had.

    I'm sorry for posting no code, but I don't see how posting it here would help with what I need to know.

    Thanks!


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Allowing user to input variable.

    There are many ways to skin a cat!
    Check out this:
    Java Tips - How to read input from console

    Otherwise, if you wanted something bigger/permanently store values and go beyond the basics, then I suggest reading a Java programming book or online resources, and generate something amazing.

    Hope this helps.

  3. The Following User Says Thank You to g000we For This Useful Post:

    That1guy (November 15th, 2010)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Allowing user to input variable.

    Or better yet, try - http://www.javaprogrammingforums.com...ner-class.html

    There are lots of examples in our Code Snippets forum - Java Code Snippets and Tutorials - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    That1guy (November 15th, 2010)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Allowing user to input variable.

    Quote Originally Posted by That1guy View Post
    Hello, I'm new to programming, and could use a little help with something!

    I feel like I'm missing the obvious answer, but I just cant think of it!

    I'm trying to allow the user to input 5 variables when the program prompts them, and then these variables will be used later on as the number amount of fabricated items that the user has to use.

    For Example:
     


    "Hello! Input the number of Arrows you want!"

    "=user inputs #="

    "You have # arrows! How many arrows would you like to use?"

    "user inputs #"



    I need the number the user inputted to be a variable, so that the user cant use more of the item than they inputed that they had.

    I'm sorry for posting no code, but I don't see how posting it here would help with what I need to know.

    Thanks!
    Scanner console = new Scanner(System.in);
    int numOfArrows = 0;
    int numberOfArrowsToUse = 0;
    System.out.println("Hello! Input the number of Arrows you want!");
    numOfArrows = console.nextInt();

    System.out.println("You have " + numOfArrows + " arrows! How many arrows would you like to use?");
    numOfArrowsToUse = console.nextInt();

    However, if you're using JOptionPane, then

    int numOfArrows = 0;
    int numberOfArrowsToUse = 0;
    // JOptionPane.showInputDialog( Component parent, String message, Object title, int messageType);
    String str = JOptionPane.showInputDialog(null, "Hello! Input the number of arrows you want!", "Input number of arrows", JOptionPane.QUESTION_MESSAGE);
    numOfArrows = Integer.parseInt(str);
    String str2 = JOptionPane.showInputDialog(null, "You have " + numOfArrows + " arrows! How many arrows would you like to use?", "How many do you want to use?", JOptionPane.QUESTION_MESSAGE);

    numberOfArrowsToUse = Integer.parseInt(str2);

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    That1guy (November 15th, 2010)

  8. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Allowing user to input variable.

    Thank you all!

    I used the Arrow simulation provided by javapenguin above; and after a little tweaking, got it to run perfectly. I then used that to start working on what I actually have in mind; but when compling it, I keep running into errors; and I cant quite figure out what I have wrong.

    import java.util.Scanner;
     
    class Cafe
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
     
    		int mealA = 0;
    		int mealB = 0;
    		int mealC = 0;
    		int mealD = 0;
    		int mealE = 0;
    		int mealtoeat = 0;
     
     
    		System.out.println("Please tell me what first food you want to make!");
    		foodA = scan.nextLine();
    		System.out.println("Please tell me what second food you want to make!");
    		foodB = scan.nextLine();
    		System.out.println("Please tell me what third food you want to make!");
    		foodC = scan.nextLine();
    		System.out.println("Please tell me what fourth food you want to make!");
    		foodD = scan.nextLine();
    		System.out.println("Please tell me what fifth food you want to make!");
    		foodE = scan.nextLine();
     
    		System.out.println("How many " + foodA + " do you want to make?");
    		meal1 = scan.nextInt();
    		System.out.println("How many " + foodB + " do you want to make?");
    		meal2 = scan.nextInt();
    		System.out.println("How many " + foodC + " do you want to make?");
    		meal3 = scan.nextInt();
    		System.out.println("How many " + foodD + " do you want to make?");
    		meal4 = scan.nextInt();
    		System.out.println("How many " + foodE + " do you want to make?");
    		meal5 = scan.nextInt();
    		{
     
    			System.out.println(foodA + " " + mealA);
    			System.out.println(foodB + " " + mealB);
    			System.out.println(foodC + " " + mealC);		
    			System.out.println(foodD + " " + mealD);
    			System.out.println(foodE + " " + mealE);
     
    			System.out.println("What would you like to eat?");
     
    			//How do I make sure that the user can only enter one of the foods they chose to make?
     
    			System.out.println("You have " + //??? + " of " + //??? + "! How many 
     + //??? + " would you like to eat?");
    			mealtoeat = scan.nextInt();
    		}
    	}
    }

    I got alot of errors around the "How many ___ do you want to make?" parts of it.

    Also, as you can see by my notes left in, I cant quite figure how to make sure that the user can only input the foods they chose to make. If someone could direct me to somplace where I can learn more about that; it would be much appriciated.

    Thanks again!

  9. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Allowing user to input variable.

    get rid of the set of brackets that start after meal5 = scan.nextInt() and that end after mealtoeat = scan.nextInt(), those aren't needed and will cause errors.
    try adding
    String foodA = " ";
    String foodB = " ";
    String foodC = " ";
    String foodD = " ";
    String foodE = " ";
    int mealtoeat2 = 0;
    and
    int meal1 = 0;
    int meal2 = 0;
    int meal3 = 0;
    int meal4 = 0;
    int meal5  = 0;
     
    int choice =0;
     
    System.out.println("Choose the meal you want to eat.  1 = " + foodlA + " 2 = " + foodlB + " 3 = " + foodC + " 4 = " + foodD + " 5 = " + foodE + ".");
     
    boolean isAValidMeal = false;
     
    while (isAValidMeal ==false)
    {
    mealtoeat = scan.nextInt();
     
    if (mealtoeat==1)
    {
    what you'll do when it's 1
    isAValidMeal = true;
    }
     
    else if (mealtoeat ==2)
    {
    what you'll do when it's 2
    isAValidMeal = true;
    }
     
    else if (mealtoeat ==3)
    {
    what you'll do when it's 3
    isAValidMeal = true;
    }
     
    else if (mealtoeat ==4)
    {
    what you'll do when it's 4
    isAValidMeal = true;
    }
     
    else if (mealtoeat ==5)
    {
    what you'll do when it's 5
    isAValidMeal =true;
    }
     
    else
    {
    System.out.println("Enter a value between 1 and 5.");
    isAValidMeal = false;
    }
    }

    I'm assuming you're asking how many of a certain meal type of a certain food you'd like to eat.

    If so, within each if and else if statement, have, for example, for the first if statement

    boolean isAValidNumber = false;
     
    while (isAValidNumber == false)
    {
    System.out.println("You have " + meal1 + " of  " + mealA + "! How many " + mealA + " would you like to eat?");
    mealtoeat2 = console.nextInt();
     
    if (mealtoeat2 > 0 && mealtoeat2 <= meal1)
    {
    it works and do what you have to do
    isAValidNumber = true;
    }
     
    else
    {
    System.out.println("Enter a number at least 0 and at most the number of meals you made for that type.");
    isAValidNumber = false;
    }
    }


    You'll have to do something similar for the other ones.
    Last edited by javapenguin; November 16th, 2010 at 12:43 AM. Reason: Adding code block

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  3. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  4. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 01:14 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM