如何将多个数据(数组)存储到Scanner的方法中?

时间:2021-03-01 15:57:11

I'm trying to get multiple data from user using a Scanner for subject details. For example: user inputs number 3 then user has to input subject name, subject code and subject fee 3 times and I want the details to be stored in an array and last to be displayed.

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

EDIT: Thanks to @Ksap the code runs fine but the small issue is the first values entered is not displayed(null) only last input is displayed. screenshot: 如何将多个数据(数组)存储到Scanner的方法中?

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

so far this is subject class:

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

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

and the SubjectDriver:

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

}

your help will be much appreciable

你的帮助会非常明显

1 个解决方案

#1


I assume you got Nullpointerexception because of uninitialized arrays, to solve that add these lines on top of the for loop

我假设你因为未初始化的数组而得到Nullpointerexception,以解决在for循环之上添加这些行的问题

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

and looks like your Scanner also won't work as expected because of mix use of nextInt and nextLine, to solve this

看起来你的扫描仪也不会按预期工作,因为混合使用nextInt和nextLine来解决这个问题

replace subjectAmount=input.nextInt(); by this subjectAmount = Integer.parseInt(input.nextLine()) and a.subjectFee[index]=input.nextDouble() by this a.subjectFee[index] = Double.parseDouble(input.nextLine());

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

Edit: When printing the array it will print the reference of the array, if we want to view the content of the array convert it as List and print it like this Arrays.asList(a.getSubjectName()) so the print statement will look like this

编辑:当打印数组时,它将打印数组的引用,如果我们要查看数组的内容将其转换为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()));

for your information, subjectFee is the primitive double array so it will still print the reference, to view the value, declare it as Double array.

为了您的信息,subjectFee是原始的双数组,因此它仍然会打印引用,查看值,将其声明为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()));
    }
}

#1


I assume you got Nullpointerexception because of uninitialized arrays, to solve that add these lines on top of the for loop

我假设你因为未初始化的数组而得到Nullpointerexception,以解决在for循环之上添加这些行的问题

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

and looks like your Scanner also won't work as expected because of mix use of nextInt and nextLine, to solve this

看起来你的扫描仪也不会按预期工作,因为混合使用nextInt和nextLine来解决这个问题

replace subjectAmount=input.nextInt(); by this subjectAmount = Integer.parseInt(input.nextLine()) and a.subjectFee[index]=input.nextDouble() by this a.subjectFee[index] = Double.parseDouble(input.nextLine());

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

Edit: When printing the array it will print the reference of the array, if we want to view the content of the array convert it as List and print it like this Arrays.asList(a.getSubjectName()) so the print statement will look like this

编辑:当打印数组时,它将打印数组的引用,如果我们要查看数组的内容将其转换为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()));

for your information, subjectFee is the primitive double array so it will still print the reference, to view the value, declare it as Double array.

为了您的信息,subjectFee是原始的双数组,因此它仍然会打印引用,查看值,将其声明为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()));
    }
}