Java程序找不到符号nextInt()或nextLine()

时间:2017-02-27 21:19:34

标签: java arrays

这是我到目前为止编写的代码....基本上我想输入名字(最后是姓氏和GPA)并将其保存在2D数组中。我将用户输入添加到数组中时遇到了问题。在public static void addstudent下这是一个添加到数组的正确方法吗?如果是这样,为什么我一直收到找不到nextInt的错误?如果我将它改为nextLine或者只是next(),我也会得到它。谢谢你的帮助!

import static java.lang.System.in;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

public class Project1 {

public static void main (String[] args){
Scanner input = new Scanner(System.in);
String [][] students = new String [50][3];
System.out.println("Menu");
System.out.println("A) Add Student");
System.out.println("D) Delete Student");
System.out.println("L) List Students");
String opt = input.nextLine();
char optc = opt.charAt(0);
switch(optc) {
    case 'A': addStudent(students);
    case 'D': deleteStudent(students);
    case 'L': listStudent(students);
}
}
public static void addStudent(String students[][]) {
 Scanner input = new Scanner(System.in); {
 }
 for (int i = 0; i < students.length; i++) {
     System.out.println("Please Enter first name"); 
     students[i] = in.nextInt();
 }

1 个答案:

答案 0 :(得分:0)

迟早,你想要将你的二维数组重构成一个对象数组,如下所示:

public class Student{
    public String firstName;
    public String lastName;
    public float GPA;

    public Student(String fn, String ln, float g){
        firstName = fn;
        lastName = ln;
        GPA = g;
    }
}

public class project1{
//note that the variables are class-scope, not function-scope
    Student stus[50];
.
.
.
}

与此同时,你试图访问一个二维数组,而不是一个1d数组,你需要接受一个字符串而不是一个int,所以这一行:

     students[i] = in.nextInt();

应如下所示:

    students[i][0] = input.nextLine();