【问题标题】:How to store multiple data (array) to a method from Scanner?如何将多个数据(数组)存储到 Scanner 的方法中?
【发布时间】:2015-05-09 13:37:16
【问题描述】:

我正在尝试使用Scanner 从用户那里获取多个数据以获取主题详细信息。例如:用户输入数字 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;
}
}

和 SubjectDriver:

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()));
}

}

您的帮助将非常显着

【问题讨论】:

  • 它给出的错误是什么?
  • 为什么不使用数组而不是使用ArrayList
  • 你有什么问题..?
  • 此错误:praactiveArray.SubjectDriver.main(SubjectDriver.java:20) 处的线程“main”java.lang.NullPointerException 中出现异常

标签: java arrays string methods


【解决方案1】:

我假设您因为未初始化的数组而得到了Nullpointerexception,以解决将这些行添加到for loop 之上的问题

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

并且看起来您的 Scanner 也无法按预期工作,因为混合使用 nextIntnextLine 来解决这个问题

用这个subjectAmount = Integer.parseInt(input.nextLine())替换subjectAmount=input.nextInt();a.subjectFee[index]=input.nextDouble() 通过这个a.subjectFee[index] = Double.parseDouble(input.nextLine());

编辑: 打印array时会打印array的引用,如果我们想查看数组的内容,将其转换为List并像这样打印 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()));

供您参考,subjectFeeprimitive 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()));
    }
}

【讨论】:

  • 非常感谢 :) 它可以工作,但它可能不显示 System.out.println(),你知道吗?
  • 到目前为止,这是:主题名称:[null, null, null, python] 主题代码:[null, null, null, c444] 主题费用:[[D@5c647e05]
  • 我的意思是为什么它不显示所有的输入?它只显示最后一个输入
  • @OTAMD 我能知道你输入的输入值吗
  • 我的输入如下: 请输入您负责的科目数量?(最多4个) 2 输入科目名称:java 输入科目代码:c222 输入科目费用:1200 输入科目名称:php输入主题代码:c444 输入主题费用:1200 主题名称:[null, php] 主题代码:[null, c444] 主题费用:[[D@5c647e05] ......第一次输入显示为null!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 2016-07-10
  • 2021-04-09
  • 1970-01-01
相关资源
最近更新 更多