Search code examples
javaloopsfencepost

For or While loop skipping an input


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.


Solution

  • 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 :)