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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:52:44+00:00 2026-06-12T13:52:44+00:00

Possible Duplicate: Scanner issue when using nextLine after nextInt I am creating a client

  • 0

Possible Duplicate:
Scanner issue when using nextLine after nextInt

I am creating a client program that needs to read both a String and an integer from my server. Depending on what integer it receives it adds some labels to the GUI. So far, my program reads the integer but skips the String. The following output is the output of my program when I try to write the integers to the program:

  • Server writes: 1
  • Server writes: 1
  • System prints: 1
  • System prints: j1
  • System prints: Name

The problem is that I am unable to write a String because it skips the String. How can I avoid this problem (note that I have also tried a for loop)

My code is as following:

int times = client.reciveCommando();
int o = 0;
System.out.println(times);

while (o != times) {
  int j = client.reciveCommando();
  System.out.println("j"+ j);
  String name = client.reciveString();
  System.out.println("Name " +name);
  createUser(j, name);
  o++;

}

The createUser method:

private void createUser(int j, String reciveChat) {
  if (j == 1) {
    chatPerson1.setVisible(true);
    lbl_Chatperson1_userName.setVisible(true);
    lbl_Chatperson1_userName.setText(reciveChat);
  } else if (j == 2) {
    lbl_chatPerson2.setVisible(true);
    lbl_userName2.setVisible(true);
    lbl_userName2.setText(reciveChat);
  } else {
    chatPerson3.setVisible(true);
    lbl_userName3.setVisible(true);
    lbl_userName3.setText(reciveChat);
  }
}

The client.reciveCommando method:

public int reciveCommando() throws IOException{
  Integer i = input.nextInt();
  return i;
}

The client.reciveString method:

public String reciveString(){
  String x = input.nextLine();
  return x;
}

Hope someone is able to help me with this 🙂

Thank you in advance.

  • 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-12T13:52:45+00:00Added an answer on June 12, 2026 at 1:52 pm

    i found the solution to my question it turned out to be quite simple!

    first of all let me explain what i ment.

    When i my program ran the while loop it basicly skipped the line where it should have recived an input from the server. i found that the reason it did that was that the input.nextLine(); was empty which makes sence when you read the api for input.nextLine();

    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
    Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
    Returns:
    the line that was skipped

    since the line that i tried to get was an empty line it would skip and set name to ” “.

    here is the full complete code for my program and it currently works:

    The while loop:

                while (o != times) {
                    int j = client.reciveCommando();
                    System.out.println("j"+ j);
                        String name = client.reciveString();
                        System.out.println("Name " +name);
                        createUser(j, name);    
                    o++;
                }
    

    The client.reciveString();

        public String reciveString(){
    
        String x = input.next();
        return x;
    }
    

    The createUser();

        private void createUser(int j, String reciveChat) {
        if (j == 1) {
            chatPerson1.setVisible(true);
            lbl_Chatperson1_userName.setVisible(true);
            lbl_Chatperson1_userName.setText(reciveChat);
    
        }else if (j == 2) {
            lbl_chatPerson2.setVisible(true);
            lbl_userName2.setVisible(true);
            lbl_userName2.setText(reciveChat);
        }else if (j == 3){
            chatPerson3.setVisible(true);
            lbl_userName3.setVisible(true);
            lbl_userName3.setText(reciveChat);}
    

    Thank you for all of your responses and i will be sure to vote you up 🙂

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

Sidebar

Related Questions

Possible Duplicate: Scanner issue when using nextLine after nextInt Why is the empty sc.nextLine();
Possible Duplicate: How do I compare strings in Java? I wrote a program that
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: What are the stages of compilation of a C++ program? I find
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: How to use iPhone Camera as Bar Code Scanner? hi all How
Possible Duplicate: java arraylist to store userinput Hi using the code in java how
Possible Duplicate: Read large amount of data from file in Java I have a

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.