StringBuilder append in for loop in Java

When I execute the following:

static void Append() {
    StringBuilder sb = new StringBuilder();
    System.out.print("How many words do you want to append? ");
    int n = input.nextInt();
    System.out.println("Please type the words you want to append: ");
    for (int c = 1; c <= n; c++) {
        String str = input.nextLine();
        sb.append(str);
        System.out.print(" ");
    }
    System.out.print(sb);

}

Let's say if I put 3, then the computer lets me type only 2 words..

This is the output:

How many words do you want to append? 3
Please type the words you want to append: 
 I
 am
 Iam

Also, why is there a space before the words? the print function is after the input function. So shouldn't it be the opposite?

4 answers

  • answered 2018-07-11 03:34 Sivabalakrishnan

    You should replace nextLine() by next().

    import java.util.Scanner;
    public class Main
    {
    static void Append() {
        Scanner input = new Scanner(System.in);
    StringBuilder sb = new StringBuilder();
    System.out.print("How many words do you want to append? ");
    int n = input.nextInt();
    System.out.println("Please type the words you want to append: ");
    String str = null;
    for (int c = 0; c < n; c++) {
         str = input.next();
        sb.append(str +" " );
    
    }
    System.out.print(sb);
    }
    public static void main(String[] args) {
        System.out.println("Hello World");
        Append();
    }
    }
    

  • answered 2018-07-11 03:36 Geno1024

    If you debug that program, you can find that the first time of loop will get input.nextLine() with an empty string. This is when the problem occurs.

    When you input a 3 and a \n for int n = input.nextInt();, the input buffer contains "3\n", and input.nextInt(); will just take that "3", like the image below:

    the position is 1 where the position of input is 1, remaining the "\n" in the buffer. Then when the program required for nextLine(), it will read the buffer until a "\n", which results in reading an empty string.

    the position changed to 2 after a nextLine()

    So a possible workaround is to add a String empty = input.nextLine(); before the loop, or use input.next(); instead of input.nextLine();, since in the document says, input.next(); will return the next token.

    Update: Notice that no one answers your second question in the bottom...

    You should modify the line System.out.println(" "); in the loop into sb.append(" ");.

  • answered 2018-07-11 03:49 Yihang Wang

    I think it is because it read a line changing char into the string so it consider the changing line as the first and the first string is taken. you could only have two string to input

  • answered 2018-07-11 03:55 Khamit Mateyev

    If you put code printing line which was read from input as follows:

    static void append() {
      Scanner input = new Scanner(System.in);
      StringBuilder sb = new StringBuilder();
      System.out.print("How many words do you want to append? ");
      int n = input.nextInt();
      System.out.println("Please type the words you want to append: ");
    
      for (int c = 1; c <= n; c++) {
        String str = input.nextLine();
        System.out.println("input str=" + str); //pay attention to this line
        sb.append(str);
        System.out.print(" ");
      }
      System.out.print(sb);
    }
    

    you will see that first iteration does not read from input. Because there is already \n in buffer which was read with nextInt.

    To solve that you can skip line after nextInt as in code bellow (I am not sure that it is best solution):

    static void append() {
      Scanner input = new Scanner(System.in);
      StringBuilder sb = new StringBuilder();
      System.out.print("How many words do you want to append? ");
      int n = input.nextInt();
      System.out.println("Please type the words you want to append: ");
    
      if (input.hasNextLine()) input.nextLine();
    
      for (int c = 1; c <= n; c++) {
        String str = input.nextLine();
        System.out.println("input str=" + str);
        sb.append(str);
        System.out.print(" ");
      }
      System.out.print(sb);
    }
    

    Using next() is not solution, if you want read sentences as single string.