0

I will keep this short and simple, here's the program-

class Sample{
private int n;
public void getDetails(){
    Scanner y=new Scanner(System.in);
    n=y.nextInt();
    System.out.println("Entered 'n' = "+n);
}
public void displayDetails(){
    int i,j;
    int arr[]=new int[n];
    Scanner x=new Scanner(System.in);
    for(j=0;j<n;j++) {
        arr[j] = x.nextInt();
            System.out.println("Entered element = "+arr[j]);
    }
    System.out.println("Entered array: ");
    for(i=0;i<n;i++)
        System.out.print(arr[i]+" ");
    }
}

public class TestClass {
    public static void main(String[] args) {
        Sample obj = new Sample();
        obj.getDetails();
        obj.displayDetails();
        }
    }

This simple program just takes number of elements(n) and the elements of array(arr[]) as input in different methods. When the input is given in interactive mode, everything works fine. Here's the console output-

5
Entered 'n' = 5
1 2 3 4 5
Entered element = 1
Entered element = 2
Entered element = 3
Entered element = 4
Entered element = 5
Entered array: 
1 2 3 4 5 

But when I give it as Stdin input(or all input at once), it just takes the number of elements(n) and ignores my array input(arr[]). Here I had to give the array elements again. Console output-

5
1 2 3 4 5Entered 'n' = 5
1
Entered element = 1
2
Entered element = 2
3 4 5
Entered element = 3
Entered element = 4
Entered element = 5
Entered array: 
1 2 3 4 5 

I have no idea what is happening. Is it a bug? Please help

S4rt-H4K
  • 99
  • 6

2 Answers2

0

You areusing scanner to take userinput.

 Scanner y=new Scanner(System.in);

In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() doesn’t not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next().

In BufferReader class there is no such type of problem. This problem occurs only for Scanner class, due to nextXXX() methods ignore newline character and nextLine() only reads newline character. If we use one more call of nextLine() method between nextXXX() and nextLine(), then this problem will not occur because nextLine() will consume the newline character.

Please check this SO link it has good explaination.

Mahesh_Loya
  • 2,353
  • 12
  • 27
0

Part of the problem is that you are creating multiple scanners. Create one scanner and use it everywhere, like so:

import java.util.Scanner;

public class TestClass {
    public static void main(String[] args) {
        Sample obj = new Sample();
        obj.getArraySize();
        obj.displayDetails();
    }
}

class Sample{

    private int arraySize;
    Scanner scanner = new Scanner(System.in);

    public void getArraySize(){
        System.out.print("Enter size of array: ");
        arraySize = scanner.nextInt();
        System.out.println("Entered array size = " + arraySize);
    }

    public void displayDetails(){
        //Create an integer array of size arraySize
        int intArray[] = new int[arraySize];

        //Repeatedly request integers for the array
        for( int i=0; i<arraySize; i++) {
            int nextInt = scanner.nextInt();
            intArray[i] = nextInt;
            System.out.println("Entered element = " + intArray[i]);
        }

        //Print out the entered elements
        System.out.println("Entered array: ");
        for( int i=0; i<arraySize; i++) {
            System.out.print(intArray[i]+" ");
        }

        scanner.close();
    }
}

console output:

Enter size of array: 5
Entered array size = 5
5 4 3 2 1
Entered element = 5
Entered element = 4
Entered element = 3
Entered element = 2
Entered element = 1
Entered array: 
5 4 3 2 1 

You still need to check that the next value is actually an int. If you put a different type of value in there, like a letter, it will crash. See these links for additional details:

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

Java Multiple Scanners

Why is not possible to reopen a closed (standard) stream?