Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:51+00:00 2026-06-12T00:04:51+00:00

When printing out the user input as indvidual words within a line I get

  • 0

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());
        }


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:04:52+00:00Added an answer on June 12, 2026 at 12:04 am

    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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following regex to strip out non-printing control characters from user input
I have a console app that performs a lengthy process. I am printing out
I have a simple search script that takes user input and searches across directories
How would you prompt the user for some input but timing out after N
System.out.println(Please enter the required word :); Scanner scan2 = new Scanner(System.in); String word2 =
Why is this printing out 0 back in main but 6 when it is
The javascript is printing out the HTML onto the page example below, is it
I'm printing out the stack trace in my app at various points to debug
I'm building an XML document and printing out into an indented format using the
I am having some issues printing out the subarray homemain=>image This is my JSON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.