-1

I'm missing something in accepting input from keyboard and store it in array.

This is what I tried:

    public static void main(String[] args) {
        System.out.println("Hello World");

        Scanner obj=new Scanner(System.in);
        System.out.println("enter sentence ");
        String[] inputArray = new String[10];
        int i = 0;
        while (obj.hasNext()){
           String word = (String) obj.next();
           inputArray[i] = word;
           System.out.println("word is  " + word);
           System.out.println("array is  " + inputArray[i]);

        }
        System.out.println("final array is  " + inputArray); //This line doesn't print. Why ?
     }

Why I can't print the array after while loop?

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

3 Answers3

3

That is because you are already incrementing i

change to

String word = (String) obj.next();
inputArray[i] = word;
System.out.println("word is  " + word);
System.out.println("array is  " + inputArray[i]);
// now increment
i++;

The reason that the last line is not printing is that the obj.hasNext is blocking waiting for input, you need somehow to break, also see java Scanner.hasNext() usage

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • that so stupid. Thank but I'm stuck at one more thing. Can you tell me why the last print statement after while loop isnt executing? – Teja Nandamuri Jan 16 '20 at 00:01
  • 2
    the `obj.hasNext` is blocking waiting for input, you need somehow to break, also see https://stackoverflow.com/a/42130999/2310289 and https://stackoverflow.com/questions/42381990/breaking-out-of-scanner-hasnext-loop – Scary Wombat Jan 16 '20 at 00:13
  • please add the link to your answer. I'll accept it. Thanks for your time. :) – Teja Nandamuri Jan 16 '20 at 00:16
0

You can use a for cycle.

would be something like:

for(int I; i<inputArray.length;i ++){
System.out.println("word is  " + word);
System.out.println("array is  " + inputArray[i]);

}
Pythus99
  • 3
  • 2
0

variable "i" is being incremented at wrong place so System.out.println("array is " + inputArray[I]) doesn't print any value.

Statement System.out.println("final array is " + inputArray) is not reached because while loop is never exited. Please see below code, I exited while loop after 10 entries and it would print the final array.

    public static void main(String[] args) {
        System.out.println("Hello World");

        Scanner obj=new Scanner(System.in);
        System.out.println("enter sentence ");
        String[] inputArray = new String[10];
        int i = 0;
        while (obj.hasNext()){
            if(i >= 10){
                break;
            }
            String word = (String) obj.next();
            inputArray[i] = word;
            System.out.println("word is  " + word);
            System.out.println("array is  " + inputArray[i]);
            i++; //pls ignore this dumb error

        }
        System.out.println("final array is  " + inputArray); //This line doesn't print. Why ?
        Arrays.stream(inputArray).forEach(System.out::println);
    }
Salim
  • 2,046
  • 12
  • 13