Results 1 to 5 of 5
  1. #1
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Using arrays to get user input and display it

    Hello.

    My assignment is to (using arrays) get user input for several questions and then display them back to the user.

    This is an example of what I have come up with so far, but I feel like I'm pretty sure I'm not using arrays the way my professor wants - this program is no more efficient than if I had done it without an array (probably less so because of the extra code).

    My professor mentioned using for loops with the arrays, but I just don't know where to go from here...

    XML Code:
    public class ClassName
    {
    	Scanner input = new Scanner (System.in);
    	
    	public String value1;
    	public String value2;
    	public String value3;
    	
    	String Array1 [] = new String [3];
    		
    	public void methodOne()
    	{
    		System.out.println ("Favorite color? ");
    		value1 = input.nextLine();
    		Array1[0] = value1;
    		
    		System.out.println ("Favorite food? ");
    		value2 = input.nextLine();
    		Array1[1] = value2;
    		
    		System.out.println ("Favorite place to visit? ");
    		value3 = input.nextLine();
    		Array1[2] = value3;
    		
    		System.out.println (Array1[0]);
    		System.out.println (Array1[1]);
    		System.out.println (Array1[2]);
    		
    
    	}
    Thanks in advance.

    - MC
    Last edited by mc54321; 10-28-2012 at 10:21 PM.

  2. #2
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Using arrays to get user input and display it

    i find this guys videos very good. Just seach this channel for java. Good info on loops and iterators
    Jose Vidal - YouTube

  3. #3
    Jakeob is offline Java Apprentice
    Join Date
    Oct 2012
    Location
    On my computer
    Posts
    10
    Rep Power
    0

    Default Re: Using arrays to get user input and display it

    You could use a loop to cycle through each answer. Here is an example of using a for loop:

    /code
    for(int i=0;i<3;i++){
    System.out.println(Array1[i]);
    }
    //code

    This would output everything in array 0, 1, and 2.

  4. #4
    Jakeob is offline Java Apprentice
    Join Date
    Oct 2012
    Location
    On my computer
    Posts
    10
    Rep Power
    0

    Default Re: Using arrays to get user input and display it

    I typed the code thing wrong, and I can't edit it, so here it is again. :P

    Java Code:
    for(int i=0;i<3;i++){
     System.out.println(Array1[i]);
     }

  5. #5
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Using arrays to get user input and display it

    Thank you for your suggestions. They worked well for me.

Similar Threads

  1. Arrays made from user input
    By sonofJack3 in forum New To Java
    Replies: 15
    Last Post: 12-01-2011, 07:18 AM
  2. Replies: 2
    Last Post: 09-27-2011, 07:46 PM
  3. Replies: 4
    Last Post: 03-09-2011, 06:54 AM
  4. Replies: 1
    Last Post: 10-18-2010, 08:14 AM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 02:23 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
  •