简体   繁体   English

二维字符串数组跳过第一个值

[英]two dimensional String array skipping first value

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:第一个代码的结果:

在此处输入图像描述

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.nextInt()方法的next()之后第一次使用nextLine时,它只读取最后一行的 EOL,而不是下一行,正如您自己注意到的那样,下一行就可以了。 for solving this, you should just do an sc.nextLine();为了解决这个问题,你应该只做一个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()? 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

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

See this question: Scanner is skipping nextLine() after using next() or nextFoo()?看到这个问题: 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.相反,它在第一列中存储了一个空字符串,这就是您在 output 中看不到任何内容的原因。 This is caused by nextInt() not reading in the final newline character when you asked for the number of students.这是由于当您询问学生人数时nextInt()没有读取最后的换行符造成的。 You can work around this by adding a call to scanner.nextLine() after your nextInt() call, like such:您可以通过在nextInt()调用之后添加对scanner.nextLine()的调用来解决此问题,如下所示:

  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.这将消耗换行符,使循环中对nextLine()的调用实际上返回正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM