0

I am trying to set arrays based on user input but I have an main issue. Here is my code:

    Scanner myScanner = new Scanner(new BufferedInputStream(System.in));
    System.out.println("Please enter your text");
    ArrayList myArray = new ArrayList();
    while (myScanner.hasNext()){
    myArray.add(myScanner.nextLine());

        for (int i = 0; i <myArray.size() ; i++) {
            System.out.println(myArray.get(i));
        }

    }

When I insert

    a
    b
    c

I just get

    a
    a
    b

Also is there a better way to print my arrays?

Edit:

When I change my code like this

    while (myScanner.hasNext()){
        myArray.add(myScanner.nextLine());
       break;
    }
    for (int i = 0; i <myArray.size() ; i++) {
        System.out.println(myArray.get(i));
    }

I just get a as a result.

4 Answers 4

3

You are printing the entire array each time you go through the while loop. In the first iteration of the while loop the for loop prints out a, the second time through the while loop, the for loop print out a (again) and b.

Move the for loop out of the while loop to get the expected output as below:

while (myScanner.hasNext()){
  myArray.add(myScanner.nextLine());

} // end of while

for (int i = 0; i <myArray.size() ; i++) {
  System.out.println(myArray.get(i));
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this one before writing here but I can't get any returns. It is stucked in there. Then I added a break; in while loop but thist me it just gives me the first line as an array.
@MuharremYelmer As I mentioned in your answer, the while loop is always waiting on the myScanner.hasNext() condition. Adding a break will simply break after the first iteration, and will output the one input you have so far.
@Timtech I tried to add a break thats working well but how can I get other 2 entries ?
@MuharremYelmer If you know you only want to have 3 entries, you should use a for loop to loop exactly 3 times.
@MuharremYelmer Well, obviously then you have to change the condition of the loop to check for a signal that you have given all of the entries. As it is now, your program's while loop has no idea where to stop.
|
2

Every time you get the next line from your scanner, it increases array size by one. So, entering the alphabet, you would get a,b,c,d,e... and so on.

After every input, your for loop outputs the entire array. So, the first time the array will have a, the second time it will have a,b, and so on. If you went on, your output would continue in this format a,a,b,a,b,c,a,b,c,d.... The reason you didn't have a,b,c outputted yet because it was waiting for the myScanner.hasNext() condition of your while loop.

Hope this helps, feel free to ask for clarification.

1 Comment

I forgot to mention that moving the for loop outside of the while loop will give you the intended results, but the other answers have addressed that by now.
0

The duplication occurs because you are attempting to print all the values of the ArrayList myArray over and over again. So when the user inputs A, the myArray will store A then the for loop prints A, then on the next line, when the user inputs B, myArray = A,B, the for loop prints A and B and so on. Put the for loop outside the while loop which gets all the input of the user, let the user finished all the inputs and you must have a way for the user to get out of the loop like type 'Q' to exit.

Comments

0

Others have answered the first question, so I'll answer the second: There's no objectively "better" way to print an ArrayList. But here's a more concise equivalent to your loop:

myArray.forEach(System.out::println);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.