Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解决字符串矩阵索引问题?_Java_Arrays - Fatal编程技术网

Java 如何解决字符串矩阵索引问题?

Java 如何解决字符串矩阵索引问题?,java,arrays,Java,Arrays,我试图以2d数组的形式存储人员的姓名和地址,但当我运行代码时,它只接受较少的值。例如,如果我给数组2行2列,它只接受3个值 我试过在其他论坛上搜索,但找不到正确的答案。 我还更改了尺寸值,但它只给出了错误的结果 import java.util.*; class findme{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); System.out.pr

我试图以2d数组的形式存储人员的姓名和地址,但当我运行代码时,它只接受较少的值。例如,如果我给数组2行2列,它只接受3个值

我试过在其他论坛上搜索,但找不到正确的答案。 我还更改了尺寸值,但它只给出了错误的结果

import java.util.*;
class findme{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.print("enter the number of person: ");
        int per=scan.nextInt();
        System.out.print("enter the number of address: ");
        int addr=scan.nextInt();
        String addrs[][]=new String[per][addr];
        for(int i=0;i<per;i++){
            for(int j=0;j<addr;j++){
                addrs[i][j]=scan.nextLine();
            }
        }
    }
}

当按enter键获取int addr=scan.nextInt时,读取了4个值,但其中一个是空行

一个快速的解决办法是读那条空行

import java.util.*;
class findme{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.print("enter the number of person: ");
        int per=scan.nextInt();
        System.out.print("enter the number of address: ");
        int addr=scan.nextInt();
--->        scan.nextLine();
        String addrs[][]=new String[per][addr];
        for(int i=0;i<per;i++){
            for(int j=0;j<addr;j++){
                addrs[i][j]=scan.nextLine();
            }
        }
    }
}
编辑:


或者您可以使用scanner.skip

除此之外,这里的另一个答案是您的代码应该是什么样子的:

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of people: ");
    int iPeople = scanner.nextInt();

    System.out.print("Enter the number of address: ");
    int iAdresses =scanner.nextInt();

    scanner.nextLine();

    String data[][] = new String[iPeople][iAdresses];
    for(int i=0; i < iPeople; i++)
    {
        for(int j=0; j < iAdresses; j++)
        {
            System.out.printf("Enter %d address for person %d:%n", j + 1, i + 1);
            data[i][j] = scanner.nextLine();
        }
    }
并尝试遵循这些惯例:

通过在您觉得太杂乱的行之间提供适当的空行,或者根据他们试图完成的操作属于不同的组,从而使您的代码更加完整。
如果您想了解原因,请在此处解释nextLine的行为:


您也可以用nextLine替换nextLine以避免这种情况。

这可能是由于您使用了Scanner API。请使用类的java命名约定以大写开头。FindMe或FindMe在这种情况下可能是