0

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?

Ke Ke
  • 63
  • 6

4 Answers4

1

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();
}
}
Sivabalakrishnan
  • 325
  • 1
  • 3
  • 19
0

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(" ");.

Geno Chen
  • 3,835
  • 6
  • 16
  • 32
  • Thats what i exactly thought. So i inserted nextline after nextInt but still let me only 2 words if i type 3 – Ke Ke Jul 11 '18 at 03:48
  • @KeKe I also tried add a `String empty = input.nextLine();` before the loop, and it is showing the expected result for me. Is your program didn't be recompiled? – Geno Chen Jul 11 '18 at 03:51
  • Nope i was dumb. I was inserting PRINTLN the whole time when i intended to insert nextLine lmao. Thanks for answering this dumb question! – Ke Ke Jul 11 '18 at 04:05
  • I updated the answer about your question in the bottom. – Geno Chen Jul 11 '18 at 04:22
0

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

0

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.

Khamit Mateyev
  • 317
  • 1
  • 4