1

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?

1
  • @NickLH Yes. Although its not in the code I provided I've initialized scanner. Commented Oct 31, 2011 at 14:57

3 Answers 3

1

I think it is not the right way you do it. You mix up the entity Student with input related information. I'd redesign it as follows (a slightly reduced Student):

/* just an entity, no logic at all */
public class Student {
    private String name = null;
    private Integer year = null;

    public void setName(String name){
        this.name = name;
    }

    public void setYear(Integer year){
        this.year = year;
    }
}

And the logic to obtain user input:

List<Student> students = new ArrayList<Student>();
int numberOfStudents = 3;

for (int i = 0; i < numberOfStudents; i++){
    System.out.println("Please enter name:");
    String name = sc.nextLine();

    System.out.println("Please enter year:");
    Integer year = sc.nextInt();

    /* after gathering the input create student */
    Student s = new Student();
    s.setName(name);
    s.setYear(year);

    students.add(s);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This is just for the name. I have to do the same with the dates. Right? Didn't learn Arraylist (just arrays) so can't fully understand the code. Sorry
Exactly, slightly modified my answer - added year. Btw: Think the object oriented way :-)
0

What about saving the Student objects themselves in a list?

List<Student> studentList = new ArrayList<Student>();
studentList.add(student);

Also when you are retrieving information from the Scanner, assign those pieces of info (like name, age etc..) to a temporary object so you can do some validation on them.

String temp = sc.nextLine();

Validate "temp" and if it is what you want stored in the person's name then call setter from the person's class.

It is always a good idea to validate user input before you call any setter methods on your Object.

Comments

0

It seems that you are dividing the responsiblity of gathering the information between the code in your main block and the code in your Stundent (note the misspelling) class.

I would concentrate on thinking about what the class's responsibility is, and making sure that there is no extra code within the class which doesn't directly handle that class's responsbility. For example, if a Student is to maintain its name, then the name field should be inside of Student (you did it this way, and it makes a lot of sense). However, if a Student's name is to be updated, it doesn't make sense for the Student to prompt via a println statement that it is expecting a name after you have assigned one name to it.

Perhaps you need a StudentReader class which can read the input and then construct the Student object. Such a reader would have the exclusive responsbility of doing all the correct prompting and reading of values, with the end result being a single constructed Student. Then the main block would basically be a loop to read each student with a StudentReader and a section to collect the read Students and process them accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.