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

  • SEARCH
  • Home
  • 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 605089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:06:31+00:00 2026-05-13T17:06:31+00:00

When I run the following code it stops to let me enter servername and

  • 0

When I run the following code it stops to let me enter servername and them port number, after that it is supposed to stop and let me enter username but it never pauses for scan.nextline() and flies straight to enter reciever name??

public static void main(String[] args) throws IOException {
    try {
        System.out.println("\n\n\nTCP Chat Client\n\nEnter server name:");
        Scanner scan = new Scanner(System.in);

        //get server info from user
        serverName = scan.nextLine();

        System.out.println("\nEnter port number:");
        serverPort = scan.nextInt();


        System.out.println("Enter your username:\n");
        userName = scan.nextLine();

        //make connection to server
        cSocket = new Socket(serverName, serverPort);
        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

        //send username to server
        out.println(userName);
        String rcvrname="wefwef";
        String message=null;
        //start messaging
        while(!(rcvrname.equals("exit"))){
            System.out.println("Enter reciever name");
            out.println(scan.nextLine());
            System.out.println("Enter message");
            out.println(scan.nextLine());

        }
        out.close();
        in.close();
        cSocket.close();

    }
  • 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-05-13T17:06:31+00:00Added an answer on May 13, 2026 at 5:06 pm

    Instead of scan.nextInt(), I would use scan.nextLine() again, and then parse the input. If you get a NumberFormatException, you can bark at the user that the input was invlaid.

    The reason why you need to do something like this is that you still have a newline sitting at the end of your serverPort input. When you use nextInt(), the scanner grabs all of the next token and tries to convert it into an Integer. Anything after the Integer token remains to be scanned.

    To prove this, with your original code, if you put a token separator, like a space, after your server port input and then add some text, you will find that your subsequent userName assignment will grab from the end of the number to the next newline.

        System.out.println("\nEnter port number:");
        Integer serverPort = scan.nextInt();
        System.out.println("\nEnter your username:");
        String userName = scan.nextLine();
    
        System.out.println("\nserverPort: " + serverPort 
                         + "\nuserName: " + userName);
    

    with the input:

        Enter port number: 
        123 abc
        Enter your username:
    

    will output

       serverPort: 123
       userName:  abc
    

    This is because the space is acting as a token separator, and after the scan.nextInt() is called, the space and the abc are still left in the buffer. When the subsequent scan.nextLine() is called, the space and the abc are scanned from the buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread that executes the following code: public void run() { try
when I trying to run following code. var result = from c in db.brand
I run the following code from command line: public class MemoryTest { public static
I run the following code on a Windows platform. The purpose is the know
When I run the following Code it crashes at the last line. I don't
When I run the following code I get B as expected: class A {
If I run the following code on my HTC magic phone I get a
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I run the following code, it only seems to be downloading the first

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.