Java 如何将多个数据(数组)从扫描仪存储到一个方法?

Java 如何将多个数据(数组)从扫描仪存储到一个方法?,java,arrays,string,methods,Java,Arrays,String,Methods,我正试图使用扫描仪获取用户的多个数据,以获取主题详细信息。例如:用户输入数字3,然后用户必须输入主题名称、主题代码和主题费用3次,我希望详细信息存储在数组中,最后显示 编辑: 由于@Ksap,代码运行良好,但小问题是没有显示输入的第一个值(null),只显示最后一个输入。 截图: 到目前为止,这是一个主题类: public class Subject { public String[] subjectName; public String[] subjectCode; public Double

我正试图使用
扫描仪
获取用户的多个数据,以获取主题详细信息。例如:用户输入数字3,然后用户必须输入主题名称、主题代码和主题费用3次,我希望详细信息存储在数组中,最后显示

编辑: 由于@Ksap,代码运行良好,但小问题是没有显示输入的第一个值(null),只显示最后一个输入。 截图:

到目前为止,这是一个主题类:

public class Subject {
public String[] subjectName;
public String[] subjectCode;
public Double[] subjectFee;

  //default constructor 
  Subject(){            
    }

 Subject(String[] subjectCode, String[] subjectName, Double[] subjectFee){
     this.subjectCode = subjectCode; 
     this.subjectName = subjectName;
     this.subjectFee = subjectFee;
   }
    //**********************************Setter Methods******************************\\
public void setSubjectCode(String[] subjectCode){
     this.subjectCode=subjectCode;
}
public void setSubjectName(String[] subjectName){
     this.subjectName=subjectName;
}
public void setSubjectFee(Double[] subjectFee){ 
     this.subjectFee=subjectFee;
}
//**********************************Getter Methods******************************\\

public String[] getSubjectCode(){ 
       return subjectCode;
}
public String[] getSubjectName(){
       return subjectName;
}
public double[] getSubjectFee(){ 
       return subjectFee;
}
}
和被试驾驶员:

public class SubjectDriver {
public static void main(String[] args) {
    int subjectAmount;
    Subject a = new Subject();
    Scanner input = new Scanner(System.in);
    System.out
        .println("Please input the amount of subjects are you resposible  for ?( Maximum 4)");
    subjectAmount = Integer.parseInt(input.nextLine());

    a.subjectName = new String[subjectAmount];
    a.subjectCode = new String[subjectAmount];
    a.subjectFee = new Double[subjectAmount];

    for (int index = 0; index < subjectAmount; index++) {
        System.out.println("enter subject name: ");
        a.subjectName[index] = input.nextLine();;

        System.out.println("enter subject code: ");
        a.subjectCode[index] = input.nextLine();

        System.out.println("enter subject fee: ");
        a.subjectFee[index] = Double.parseDouble(input.nextLine());
    }

    System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
        + "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
        + Arrays.asList(a.getSubjectFee()));
}
公共类主题驱动程序{
公共静态void main(字符串[]args){
主语量;
主题a=新主题();
扫描仪输入=新扫描仪(System.in);
系统输出
.println(“请输入您负责的科目数量?(最多4个)”;
subjectAmount=Integer.parseInt(input.nextLine());
a、 subjectName=新字符串[subjectAmount];
a、 subjectCode=新字符串[subjectAmount];
a、 主题费=新的双倍[主题金额];
对于(int index=0;index
}


您的帮助将非常可观

我假设您得到了
Nullpointerexception
,因为未初始化的数组,要解决这个问题,请在
for循环的顶部添加这些行

a.subjectName = new String[subjectAmount];
a.subjectCode = new String[subjectAmount];
a.subjectFee = new double[subjectAmount];
而且由于混合使用了
nextInt
nextLine
,您的
扫描仪也无法正常工作

替换
subjectAmount=input.nextInt()subjectAmount=Integer.parseInt(input.nextLine())
a.subjectFee[index]=input.nextDouble()
a.subjectFee[index]=Double.parsetdouble(input.nextLine())

编辑: 打印
数组
时,它将打印
数组
的引用,如果我们想查看数组的内容,请将其转换为列表并按如下方式打印
Arrays.asList(a.getSubjectName())
因此,
print
语句如下所示

System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
            + "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
            + Arrays.asList(a.getSubjectFee()));
请注意,
subjectFee
原语double
数组,因此它仍将打印引用,要查看该值,请将其声明为
double
数组

public class SubjectDriver {
    public static void main(String[] args) {
        int subjectAmount;
        Subject a = new Subject();
        Scanner input = new Scanner(System.in);
        System.out
            .println("Please input the amount of subjects are you resposible  for ?( Maximum 4)");
        subjectAmount = Integer.parseInt(input.nextLine());

        a.subjectName = new String[subjectAmount];
        a.subjectCode = new String[subjectAmount];
        a.subjectFee = new Double[subjectAmount];

        for (int index = 0; index < subjectAmount; index++) {
            System.out.println("enter subject name: ");
            a.subjectName[index] = input.nextLine();;

            System.out.println("enter subject code: ");
            a.subjectCode[index] = input.nextLine();

            System.out.println("enter subject fee: ");
            a.subjectFee[index] = Double.parseDouble(input.nextLine());
        }

        System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
            + "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
            + Arrays.asList(a.getSubjectFee()));
    }
}
公共类主题驱动程序{
公共静态void main(字符串[]args){
主语量;
主题a=新主题();
扫描仪输入=新扫描仪(System.in);
系统输出
.println(“请输入您负责的科目数量?(最多4个)”;
subjectAmount=Integer.parseInt(input.nextLine());
a、 subjectName=新字符串[subjectAmount];
a、 subjectCode=新字符串[subjectAmount];
a、 主题费=新的双倍[主题金额];
对于(int index=0;index
它给出了什么错误?与其使用数组,不如使用
ArrayList
这里的问题是什么..?这个错误:线程“main”java.lang.NullPointerException在practiveArray.SubjectDriver.main(SubjectDriver.java:20)中出现异常非常感谢:)它工作起来有些许,但没有显示System.out.println()可能,你知道吗?到目前为止,这是:主题名称:[null,null,null,python]主题代码:[null,null,null,c444]主题费用:[[D@5c647e05]我的意思是为什么它不显示所有的输入?它只显示最后的输入input@OTAMD我可以知道您输入的输入值吗?我的输入如下:请输入您负责的科目数量?(最多4个)2输入主题名称:java输入主题代码:c222输入主题费用:1200输入主题名称:php输入主题代码:c444输入主题费用:1200主题名称:[null,php]主题代码:[null,c444]主题费用:[[D@5c647e05]……第一个输入显示为空!