-1

Inside the first loop, it is not taking the first word as an input. When I'm not taking input of the string length from the user, it's working fine. See the output where its not taking input the word 0.

public class Stringinput {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.println("How many words do you need to make the sentence: ");
            int n = in.nextInt();
            String[] names = new String[n];

            for (int i = 0; i < names.length; i++) {

                System.out.printf("Enter word no %d : ",i);
                names[i]= in.nextLine();
            }
            System.out.println("The sentence you made is: ");
            for (int i = 0; i < names.length; i++) {
                System.out.print(names[i]+" ");

            }
            System.out.println("");
        }
    }

And this the output:

How many words do you need to make the sentence: 
4
Enter word no 0 : Enter word no 1 : My
Enter word no 2 : name
Enter word no 3 : is
The sentence you made is: 
 My name is 

BUILD SUCCESSFUL (total time: 11 seconds)

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Mahbub
  • 5
  • 1
  • 2

3 Answers3

0

Use .next() instead of .nextLine() as you only need a single word, not a sentence because the Scanner.nextInt method does not consume the last newline character.

for (int i = 0; i < names.length; i++) {

            System.out.printf("Enter word no %d : ",i);
            names[i]= in.next();
        }

Output:

How many words do you need to make the sentence: 
4
Enter word no 0 : my
Enter word no 1 : next
Enter word no 2 : r
Enter word no 3 : t
The sentence you made is: 
my next r t 
achAmháin
  • 3,944
  • 3
  • 12
  • 39
JavaLearner1
  • 478
  • 1
  • 5
  • 18
  • This works if you pass single words without spaces. If you pass spaces it will break the loop and with nextline you can add spaces too. – Veselin Davidov May 16 '18 at 07:45
0

nextInt() does not consume the new line character entered after the number so your first nextLine() will, so you need to do

public class Stringinput {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.println("How many words do you need to make the sentence: ");
            int n = in.nextInt();
            in.nextLine(); // consume leftover new line here
            String[] names = new String[n];

            for (int i = 0; i < names.length; i++) {

                System.out.printf("Enter word no %d : ",i);
                names[i]= in.nextLine();
            }
            System.out.println("The sentence you made is: ");
            for (int i = 0; i < names.length; i++) {
                System.out.print(names[i]+" ");

            }
            System.out.println("");
        }
    }
user1
  • 214
  • 1
  • 15
0

Scanner.nextInt() method does not consume the newline character of the input so it goes to the next Scanner.nextLine. It is similar if you use nextline after next() method

Just add a dummy scanner.nextLine() before the loop and keep going.

Veselin Davidov
  • 6,886
  • 1
  • 13
  • 21