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
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
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:
app.addStudent(name, DOB, address, gender);
Should be
app.addStudent(Name, DOB, Address, Gender);
Also:
Scanner
object should be sufficient. No need for four of them.gender
instead of 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....");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With