[英]Inputting into an array using scanner class (error: incompatible types: Scanner cannot be converted to String)
I'm trying to input student details using a scanner but I keep getting this error: 我正在尝试使用扫描仪输入学生详细信息,但我不断收到此错误:
error: incompatible types: Scanner cannot be converted to String
I have 4 scanners which are 我有4个扫描仪
static Scanner name = new Scanner(System.in);
static Scanner Date = new Scanner(System.in);
static Scanner address = new Scanner(System.in);
static Scanner gender = new Scanner(System.in);
My code is as follows 我的代码如下
System.out.println("You have chosen to add a student. Please enter the following details");
System.out.println("Name: ");
String Name = name.nextLine();
System.out.println("DOB: ");
String DOB = Date.nextLine();
System.out.println("Address: ");
String Address = address.nextLine();
System.out.println("Gender: ");
String Gender = gender.nextLine();
app.addStudent(name, DOB, address, gender);
System.out.println(Name + " has been added!" + "\n" + "Returning to menu....");
app.delay();
The addStudent
method is as follows addStudent
方法如下
public void addStudent (String name,String DOB,String address,String gender)
{
for(int i = 0; i < enrolment.length; i++)
{
if (enrolment[i] == null)
{
this.enrolment[size] = new Student(name, DOB, address, gender);
this.size++;
if (gender == "Male")
{
this.maleStudents++;
}
else {
this.femaleStudents++;
}
break;
}
}
}
The problem is that you're passing your Scanner
objects to your addStudent
method instead of the strings that you obtained from the scanners: 问题是您将Scanner
对象传递给addStudent
方法而不是从扫描仪获取的字符串:
app.addStudent(name, DOB, address, gender);
Should be 应该
app.addStudent(Name, DOB, Address, Gender);
Also: 也:
Scanner
object should be sufficient. 一个Scanner
对象就足够了。 No need for four of them. 不需要四个。 gender
instead of Gender
. Java代码约定规定变量名称在较低的驼峰情况下,即gender
而不是Gender
。 Putting everything together, your code should look something like this: 将所有内容放在一起,您的代码应如下所示:
Scanner scanner = new Scanner(System.in);
System.out.println("You have chosen to add a student. Please enter the following details");
System.out.println("Name: ");
String name = scanner.nextLine();
System.out.println("DOB: ");
String dob = scanner.nextLine();
System.out.println("Address: ");
String address = scanner.nextLine();
System.out.println("Gender: ");
String gender = scanner.nextLine();
app.addStudent(name, dob, address, gender);
System.out.println(name + " has been added!" + "\n" + "Returning to menu....");
You only need one scanner, not one for each input. 您只需要一个扫描仪,而不是每个输入一个扫描仪。
In you code you are confusing Name
(the string) with name
(the scanner). 在您的代码中,您将Name
(字符串)与name
(扫描程序)混淆。
This is why there are naming rules. 这就是命名规则的原因。
Try changing to 尝试改为
Scanner input = new Scanner (System.in);
String name = input.nextLine ();
String gender = input.nextLine ();
...
then 然后
app.addStudent (name, dob, address, gender);
try also reading about naming conventions 尝试阅读有关命名约定的内容
name, adress, and gender are scanners, your method takes Strings 名称,地址和性别是扫描仪,您的方法需要使用字符串
app.addStudent(Name, DOB, Address, Gender);
is the method with the String variables that you defined 是您定义的String变量的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.