Results 1 to 3 of 3
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Making a loop and creating variables based off of user input

    Ok I'm just wondering how I would right a piece of code that will take user input and based off the integer entered knows how many variables to store. I sorta got stuck when reading my code and realized that all the variables would get stored in the same place and erased the previous int. Here's what I have....



    Java Code:
    public class AgeTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    	
    		Scanner input = new Scanner;
    		
    		int age;				// The age that was entered by user
    		int control = 0;		// The control variable
    		int amountusr;			// The amount of users at the time that will be entering ages
    		
    		System.out.println("Hello, how many users will be entering ages today?\n"); // Output this text
    		amountusr = input.nextInt()
    		
    		amountusr = control;
    		
    		int first;
    		int second;
    		int third;
    		int fourth;
    		int fifth;
    		int sixth;
    		int seventh;
    		int eight;
    		int ninth;
    		int tenth;
    		
    		while (control != 0)	{
    			System.out.println("What is your age?\n");
    			first = input.nextInt()
    			control--;
    			
    		}
    
    	}
    
    }
    I know this code is not the best and it isn't finished but I'm working on it and I have a test for my CS class and I would like to figure this one out so I can continue. Thanks everyone.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    create an int[] and make it have size input.nextInt().

    Then loop through that array, for each i, prompt user to enter there age and add that input to position i in the array

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Yes as sunde887 said use an array.

    Arrays are very fun :) and would be perfect for what you are asking.

    So a user inputs 10 for "amount of users". Now amountusr = 10. Now you can immediately create an array called users (or whatever name). And in java you need to declare an array like this.

    Java Code:
    int[] users;
    When you find out the amount of users you can add it like this.
    Java Code:
    users = new int[amountusr];
    //or
    users = new int[19];
    or the same thing.
    Java Code:
    int[] users = new int[amountusr];
    You can store integers to this array like this
    Java Code:
    users[0] = 35;
    users[1] = 25;
    users[2] = 52;
    users[3] = 16;
    so this is really cool when you use loops with them and this is what you are needing to do.

    I would try a for loop where your while loop is. You will need the array length and you can get an array length like this.

    Java Code:
    users.length;
    You will need to start at 0 so have your for loop go from 0 to user.length.

    if "i" is your for loop integer variable name then it might look like this.

    Java Code:
    for (int i = 0; i < users.length; i++) {
         users[i] = input.nextInt();
    }
    Last edited by AcousticBruce; 01-13-2011 at 05:46 AM.

Similar Threads

  1. Replies: 5
    Last Post: 11-27-2010, 03:49 AM
  2. Creating a Table with user input
    By JonniBravo in forum Eclipse
    Replies: 1
    Last Post: 09-08-2010, 12:50 PM
  3. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  4. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM

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
  •