2

When printing out the user input as indvidual words within a line I get a printout of all the words in that line.

System.out.println(userInput.next());

However, when I add the indvidual words to an ArrayList I appear to be getting random words back:

 al.add(userInput.next());

Could someone explain to me what's going on?

Thanks.

This is a full copy of the code:

import java.util.*;


public class Kwic {
    public static void main(String args[]){

        Scanner userInput = new Scanner(System.in);
        ArrayList<String> al = new ArrayList<String>();


        while(userInput.hasNext()){
            al.add(userInput.next());
            System.out.println(userInput.next());
        }


    }
}
Sheldon
  • 9,639
  • 20
  • 59
  • 96

6 Answers6

9
while(userInput.hasNext()){
    al.add(userInput.next());   //Adding userInput call to ArrayList
    System.out.println(userInput.next());  //Printing another userInput call
}

You are not printing the value stored in your ArrayList but actually another call to the userInput.next()

Revision

@Sheldon This is working for me.

public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);
    ArrayList<String> al = new ArrayList<String>();
    while(userInput.hasNext()){
        al.add(userInput.next());
        System.out.println(al);  //LINE CHANGED FROM YOUR QUESTION
    }

}

I tested your code with input 1 2 3 4 5 6 7 8 9 0

Then I pressed enter and Got:

2 4 6 8 0

the userInput.next() is alternating between the one added to the ArrayList and the one captured by your System.out.println

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • I don't think I explained myself well enough. When I use System.out.println(userInput.next()); I'm not getting all the words in the line e.g. "How are you" is not printing "How" "Are" "You" but instead just printing "are". This only happens when I'm adding the values to the ArrayList though. – Sheldon Sep 23 '12 at 18:16
  • @Sheldon If I print the arrayList instead of userInput.next() it still works. – gtgaxiola Sep 23 '12 at 18:20
  • @Sheldon I got it now... it's your while loop first entry goes to the list, second entry goes to your System.println – gtgaxiola Sep 23 '12 at 18:22
5

Because next() consumes the next token from the scanner. Thus, when you have:

        al.add(userInput.next());
        System.out.println(userInput.next());

You are actually consuming two tokens from the scanner. The first is being added to the ArrayList and the other is being printed to System.out. A possible solution is to store the token in a local variable, and then add it to the array and print it:

    while (userInput.hasNext()) {
        String token = userInput.next();
        al.add(token);
        System.out.println(token);
    }
João Silva
  • 89,303
  • 29
  • 152
  • 158
2

I'd write it this way:

import java.util.*;


public class Kwic {
    public static void main(String args[]){

        Scanner userInput = new Scanner(System.in);
        List<String> al = new ArrayList<String>();


        while(userInput.hasNext()){
                al.add(userInput.next());
        }
    System.out.println(al);

    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
1

It would be more beneficial for you to store all of the values into the ArrayList first, then print them out. What you're doing now is printing another call to userInput.next(), which may or may not be present.

while(userInput.hasNext()){
    al.add(userInput.next());
}

for(String s : al) {
    System.out.println(s);
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
0
al.add(userInput.next());   //Adding an Item call to ArrayList

System.out.println(userInput.next()); //Printing the next userInput with a call**

Try this to print the values in ArrayList

for(String s : al){

     System.out.println(s);

 }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Not only you're not printing all the words in the line, you're also not adding all the words into the ArrayList. Because the way you have coded, it adds every alternate words to the ArrayList and prints the alternate words.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186