Entering into an array using the scanner class (error: incompatible types: the scanner cannot be converted to String)

I try to enter student data using a scanner, but I keep getting this error:

error: incompatible types: Scanner cannot be converted to String

I have 4 scanners that

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 method is addStudentas 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;
        }
    }
}
+4
source share
3 answers

The problem is that you are passing your objects to Scanneryour method addStudentinstead of strings received with scanners:

app.addStudent(name, DOB, address, gender);

Must be

app.addStudent(name, DOB, address, gender);

also:

  • one object Scannershould be sufficient. There is no need for four of them.
  • Java- , , .. gender gender.

, :

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....");
+5

, .

Name () Name ().

.

 Scanner input = new Scanner (System.in);

 String name = input.nextLine ();
 String gender = input.nextLine (); 
 ...

 app.addStudent (name, dob, address, gender);

+1

, - ,

app.addStudent(Name, DOB, Address, Gender);

- ,

0

All Articles