6 Replies - 2016 Views - Last Post: 12 November 2012 - 09:29 AM Rate Topic: -----

#1 argylesocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-November 12

Can you store user input into an array multiple times?

Posted 06 November 2012 - 07:59 AM

My goal is to ask the user a series of questions, at least twice, and store their answers into an array. Then, output all contents back to the user. So, for the following example:

public void getInfo()
	{
		System.out.print("Please your favorite food: ");
		Array1[0] = input.nextLine();
		// Say user input is "Fluffy"

		System.out.print("Please enter your favorite color: ");
		Array1[1] = input.nextLine();
                // Say user input is "Blue"
		
		System.out.print("Please enter your favorite season: ");
		Array1[2] = input.nextLine();
                // Say user input it "Winter"
		
		
                System.out.println();
		for (int i=0; i < 3; i++)
		{
			System.out.println(Array1[i]);
		}
                
                // The program will output:

                // Fluffy
 	        // Blue
                // Winter
	}



What I want is to be able to ask the user the questions at least twice, and then output them both together. So if they answered the questions again with, "Spot", "Yellow", "Spring", the program would output:

Fluffy
Blue
Winter

Spot
Yellow
Spring

I am planning on using a loop to ask the questions twice, which I know how to do. But I cannot figure out how to store two sets of answers to the same questions. Can I use just one array to do this? Is there any way to keep both sets of information stored in the array without one overwriting the other? Or would a multi-dimensional array work better?

Thanks in advance for any suggestions.

Is This A Good Question/Topic? 0
  • +

Replies To: Can you store user input into an array multiple times?

#2 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 342
  • Joined: 14-May 02

Re: Can you store user input into an array multiple times?

Posted 06 November 2012 - 08:52 AM

You could use a 2d array to store your answers.

Answers[][];

So each loop through the questions would be stored in the first dimension, and each specific questions is stored in the second.

// First time through questions

Answers[0][0] = food;
Answers[0][1] = colour;
Answers[0][2] = season;

// Second time through questions
Answers[1][0] = food;
Answers[1][1] = colour;
Answers[1][2] = season;


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can you store user input into an array multiple times?

Posted 06 November 2012 - 10:23 AM

Respect Java conventions: variable names, and thus array names, start by a lower case letter
Was This Post Helpful? 0
  • +
  • -

#4 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: Can you store user input into an array multiple times?

Posted 06 November 2012 - 12:37 PM

i changed some of the code around for ya
    public void getInfo()
    {
        String container = "";
        String stop = "-0";
        System.out.println("Please enter your favorite food, if you are finished type \"-0\"");
        container = keybd.nextLine();
        while(!(container.equals(stop))){
            answerList1.add(container);
 
            
        // Say user input is "Fluffy"

        System.out.println("Please enter your favorite color: ");
        container = keybd.nextLine();
        answerList2.add(container);
                // Say user input is "Blue"
        
        System.out.println("Please enter your favorite season: ");
        container = keybd.nextLine();
        answerList3.add(container);
                // Say user input it "Winter"
        System.out.println("Please enter your favorite food, if you are finished type \"-0\"");
        container = keybd.nextLine();
     
    }
        
        for (int index=0; index < answerList1.size(); index++)
        {
            System.out.println("your favorite food: "   + answerList1.get(index));
            System.out.println("your favorite color: "  + answerList1.get(index));
            System.out.println("your favorite season: " + answerList1.get(index));
        }
                
                // The program will output:

                // Fluffy
            // Blue
                // Winter
    }
}



what i did was set up a while loop and three ArrayLists to hold the data and a local container to check the data before putting it in the ArrayList and finally a stopper to tell the computer when to stop hope that helps you.
Was This Post Helpful? 0
  • +
  • -

#5 rosskopp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-November 12

Re: Can you store user input into an array multiple times?

Posted 12 November 2012 - 03:30 AM

i have a similar issue. i also need to use an array to store multiple inputs. the problem is that my code does not place the output in the same order as it was entered into the code. how do you go about selecting different objects within the array(s)?

here is an example of my code where inputs are called.
public void displayName2(){  
    
     
    {
    System.out.println("_________________________________________________________|");
    System.out.println("\nPlease enter the name of the second salesperson: ");
    name2 = input2.nextLine();
    
    System.out.println("\nEnter the amount of sales for "+name2+" next: ");
    sales2 = numbers2.nextDouble();
    System.out.println("<___________________________++___________________________>");
        
    if (sales2<=400000){
        System.out.println(name2+", you did not reach the 80% of 500k for sales");
    }
    else{
        System.out.println(name2+", you did reach the 80% of 500k for sales");
    System.out.println("_________________________________________________________|");
    }
    }
  }


here is where the objects are displayed. if you notice, the order in which it was entered is different from when it is displayed.

public void Output1 (){
        /**
         * both (Output1 and Output2) give an overall that states the name, salary
         * the commission rate in percent, the calculated commission and the 
         * total to include fixed salary and calculated commission.
         */

    sum=sales1*NNumbers.currentCommissionRate;
    takeHome1=sum+NNumbers.fixedSalary;   
    System.out.println();
    System.out.println("^-----------------| Total Compensation |----------------^");
    System.out.println("^-----------------|--------------------|----------------^");
    System.out.println("the current salary for "+name1+" is: "+(Input.Format(NNumbers.fixedSalary)));
    System.out.println();
    System.out.println("The current commission rate is: "+(Input.PFormat(NNumbers.currentCommissionRate)));
    System.out.println();
    System.out.println("the total commission for sales is "+(Input.Format(sum)));
    System.out.println("---------------------------------------------------------");
    System.out.println("Total take home with sales and salary is ->"+(Input.Format(takeHome1))+"<-");
    System.out.println();


hope this makes sense...

This post has been edited by jon.kiparsky: 12 November 2012 - 09:25 AM
Reason for edit:: added code tags

Was This Post Helpful? 0
  • +
  • -

#6 MattDelly  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 43
  • Joined: 08-November 12

Re: Can you store user input into an array multiple times?

Posted 12 November 2012 - 09:19 AM

It would help us a lot if you used code tags my friend :)

You can access individual pieces of the array by using the index

for(int i = 0; i < 5; i++){
   num[i] = 0;
}



You can make an array of type object and access methods from it
static Car[] place = new Car[MAX];
for (Car car : place) {
    System.out.println(car.getColour()); // This gets the color of the car. You could make a get method for salary ect.
}


Was This Post Helpful? 0
  • +
  • -

#7 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Can you store user input into an array multiple times?

Posted 12 November 2012 - 09:29 AM

Each Array can hold only one element. However, you can use Array of Objects, and let each array point to another array that has like 5, 10, 100, 1000 elements.. As many as elements as you need..

View PostWolverineX, on 12 November 2012 - 09:28 AM, said:

Each Array can hold only one element. However, you can use Array of Objects, and let each array point to another array that has like 5, 10, 100, 1000 elements.. As many as elements as you need..


I mean each index of an array can hold one element.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1