1

I am trying to insert some value from user input in two dimensional String array i'm using two code to test that but both of them skipping the first column and store nothing. Here the code and the image of result.

First code:

      Scanner sc= new Scanner(System.in); 
      System.out.print("Enter Number of Students ");  
      int a= sc.nextInt();
      String [][] StudentDetail = new String[a][2];
      System.out.println("Enter Details of "+ a+ " students"); 

       for (int row = 0; row < StudentDetail.length; row++) {
       System.out.print("Enter  name: ");
       StudentDetail[row][0] = sc.nextLine();

       System.out.print("Enter a ID ");
       StudentDetail[row][1] = sc.nextLine();

       }
    System.out.println( StudentDetail[0][0] ); 
               System.out.println( StudentDetail[0][1] );
        }
    

Second code :

 for (int i=0; i<a; i++) {
           
      System.out.println("Enter Details of student "+ (i+1));
      for (int j=0;j<2;j++){
      StudentDetail[i][j] = sc.nextLine();
            
           }
       }

The result of first code:

enter image description here

mira
  • 23
  • 3

2 Answers2

1

the problem is not about array and dimensions. it's about scanner. when using nextLine for the first time after next() of nextInt() methods, it just read the EOL of last line, not the next line, as you noticed yourself, next lines will be ok. for solving this, you should just do an ‍sc.nextLine()‍; without using its return value.

your code will be like this:

      Scanner sc= new Scanner(System.in); 
      System.out.print("Enter Number of Students ");  
      int a= sc.nextInt();
      String [][] StudentDetail = new String[a][2];
      System.out.println("Enter Details of "+ a+ " students"); 
    
      sc.nextLine(); // to ignore the EOL after int in the first line

      for (int row = 0; row < StudentDetail.length; row++) {
             System.out.print("Enter  name: ");
             StudentDetail[row][0] = sc.nextLine();

             System.out.print("Enter a ID ");
             StudentDetail[row][1] = sc.nextLine();

       }
       System.out.println( StudentDetail[0][0] ); 
       System.out.println( StudentDetail[0][1] );

for more info, you may consider reading these links:

Scanner is skipping nextLine() after using next() or nextFoo()?

https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/

1

See this question: Scanner is skipping nextLine() after using next() or nextFoo()?

Basically, your code isn't skipping the first column. Instead, it stored an empty string into the first column, which is why you don't see anything in the output. This is caused by nextInt() not reading in the final newline character when you asked for the number of students. You can work around this by adding a call to scanner.nextLine() after your nextInt() call, like such:

  Scanner sc= new Scanner(System.in); 
  System.out.print("Enter Number of Students ");  
  int a= sc.nextInt();
  sc.nextLine();
  String [][] StudentDetail = new String[a][2];
  System.out.println("Enter Details of "+ a+ " students"); 

This will consume the newline character, making the calls to nextLine() in your loop actually return the proper result.

YCF_L
  • 49,027
  • 13
  • 75
  • 115
Tyler Tian
  • 557
  • 1
  • 5
  • 18