I am learning java and started with classes and I am now having some doubts.
I'm going to make a program with data from students, classes and chairs. For example information from the student data will be used in classrooms and chairs. For now, I will restrict myself to the class student.
Each student will have a name and birth date. I thought I'd create a class with student data and then save the data of students in two arrays. One-dimensional array of strings for the names and an array with three columns for the dates (year, month, day).
I started by creating the Student class with the code:
public class Stundent{
private String nameStudent;
private int yearBirth;
private int monthBirth;
private int dayBirth;
public void setName(String name){
System.out.println("Insert student's name:");
nameStudent = name;
}
public String getName(){
return nameStudent;
}
public void setYear(int year){
System.out.println("Insert student's year of birth:");
yearBirth = year;
}
public int getYear(){
return yearBirth;
}
public void setMonth(int month){
System.out.println("Insert student's month of birth:");
monthBirth = month;
}
public int getMonth(){
return monthBirth;
}
public void setDay(int day){
System.out.println("Insert student's day of birth:");
dayBirth = day;
}
public int getDay(){
return dayBirth;
}
}
Then in the main file had thought this way:
Student person = new Student();
String[] nameStudents = new String[100];
int[][] birthdayStudents = new int[100][3];
for (int i = 0; i < 3; i++){
person.setName(sc.nextLine());
nameStudents[i] = person.getName();
person.setAno(sc.nextInt());
birthdayStudents[i][1] = person.getAno();
person.setMes(sc.nextInt());
birthdayStudents[i][1] = person.getMes();
person.setDia(sc.nextInt());
birthdayStudents[i][2] = person.getDia();
}
First question:
Forgetting that this not working properly because the scanner, is this the correct way of thinking?
Second question:
If I run this code I have the following situation: It will be presented to me "Insert student's name:" but if I place Maria InputMismatchException will appear. I believe he is storing Maria in the year. Why does this happen? In the same for cycle I can't have more than one call to the scanner? If I only put in the code:
person.setName(sc.nextLine());
nameStudents[i] = person.getName();
and remove everything else concerning the birthday it will work OK. Can anyone enlighten me?