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 9224621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:22:08+00:00 2026-06-18T04:22:08+00:00

I am using Scanner class to take input. I am trying to get all

  • 0

I am using Scanner class to take input. I am trying to get all the
words in a line using in.next(). I know it can be done using
nextLine() but i want to understand how in.next() and in.hasNext()
works.

System.out.println("What is designation");
        String desg = in.next();      
        while(in.hasNext()){
            desg+=in.next();      
        }

Gives out put as

What is designation
member technical staff\n
^Z
Hello abhishek kumarNext year you will be 22Salary is 30000.0Designation is membertechnicalstaff

But if i use

System.out.println("What is designation");
        String desg = in.next();      
        if(in.hasNext()){
            desg+=in.next();      
        }

It gives output as

What is designation
member technical staff
Hello abhishek kumarNext year you will be 22Salary is 44254.0Designation is membertechnical

In the first case i am getting all the words but it keeps asking for
next input and i have to specify end of input using CTRL+Z. But in
second case i am not getting the last word(staff). Please explain.

  • 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-18T04:22:09+00:00Added an answer on June 18, 2026 at 4:22 am

    The first code reads the input in a while loop – i.e. until it finds in.hasNext() == false.

    The second code is using an if condition – it reads in.next() at most once (after the initial read).

    Thus, the second code is not “waiting for new input” because it simply asks for in.next() only once, and not until input is exhausted, unlike the second code snap.


    P.S. Note that the line String desg = in.next(); (in the first code snap) is a bad practice for two reasons:

    1. It will fail for empty input.
    2. It is a code duplication with the content of the while loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the scanner class to capture user input from the command line
I'm using the Scanner class to take some input from the user through the
I am trying to use the Scanner class to read a line using the
I am trying to have a scanner take input in a loop. Once the
I'm trying to use the Scanner class in Java to get data from a
I'm using the Scanner class in java to go through a a text file
I'm using scanner to read a text file line by line but then how
I have been trying to write a bare-bones ping scanner using Perl for internal
I am trying to read data from a bluetooth barcode scanner (KDC300) using C.
I am using Delphi and trying to read from a bar code scanner over

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.