user16052567
user16052567

Reputation:

How to print information at certain index in array from main method?

I haven't coded in over a year and I'm really struggling to remember anything. I have 100 tabs open trying to relearn but I'm truly stuck.

This is my main method.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        StudentDatabase database = new StudentDatabase();
        database.addStudent("Joe Schmo", 1101, 3.2);
        database.addStudent("Katie Katerson", 31415926, 3.7);
        database.addStudent("Watson TheBassetHound", 12345, 2.4);
        database.printDatabase();
        
        StudentDatabaseLL databaseLL = new StudentDatabaseLL();
        databaseLL.addStudent("Joe Schmo", 1101, 3.2);
        databaseLL.addStudent("Katie Katerson", 31415926, 3.7);
        databaseLL.addStudent("Watson TheBassetHound", 12345, 2.4);
        databaseLL.printDatabase();
    }
}

In another code file I need to print all the information of a student at a certain index. I literally cannot figure it out.

public class StudentDatabase {
    private Student[] database;

    public StudentDatabase() {
        database = new Student[0];
    }

    public void addStudent(String name, int idNumber, double gpa) {
        Student student = new Student(name, idNumber, gpa);
        Student[] newDatabase = new Student[database.length + 1];
        for (int i = 0; i < database.length; i++) {
            newDatabase[i] = database[i];
        }
        newDatabase[newDatabase.length - 1] = student;
        database = newDatabase;
    }

    public void printDatabase() {
        for (int i = 0; i < database.length; i++) {
            Student student = database[i];
            System.out.println(student.getName() + ": (ID=" + student.getIdNumber() + "), (gpa=" + student.getGPA() + ")");
            System.out.println();
        }  
    }

public class findStudentbyIndex {
    
    public void findStudentbyIndex(String[] args){
        int[] intArray = StudentDatabase              
                )
    }
}

This is all I have. Could someone just point me in the right direction?

Upvotes: 1

Views: 73

Answers (2)

davidvera
davidvera

Reputation: 1489

Your code seems a little bit complex for the expected result. You should not have such class like findStudentbyIndex.. the method should be part of StudentDatabase.

Case 1: I will propose a solution using your implementation You should remove then your class findStudentbyIndex. Put everything in student database.

public class StudentDatabase {
    private Student[] database;

    public StudentDatabase() {
        database = new Student[0];
    }

    public void addStudent(String name, int idNumber, double gpa) {
        Student student = new Student(idNumber, name, gpa);
        Student[] newDatabase = new Student[database.length + 1];
        for (int i = 0; i < database.length; i++) {
            newDatabase[i] = database[i];
        }
        newDatabase[newDatabase.length - 1] = student;
        database = newDatabase;
    }

    public void printDatabase() {
        for (int i = 0; i < database.length; i++) {
            Student student = database[i];
            System.out.println(student.getName() + ": (ID=" + student.getIdNumber() + "), (gpa=" + student.getGpa() + ")");
        }
    }

    public void getStudentById(int idNumber) {
        for (int i = 0; i < database.length;  i++) {
            if(database[i].getIdNumber() == idNumber) {
                System.out.println(database[i]);
            }
        }
    }

    // other version:
    public void getStudentById2(int idNumber) {
        for (Student student: database) {
            if(student.getIdNumber() == idNumber) {
                System.out.println(student);
            }
        }
    }

    // modern way
    public void getStudentById3(int idNumber) {
        System.out.println(
                Arrays.stream(database)
                        .filter(student -> student.getIdNumber() == idNumber)
                        .findFirst().get()
        );
    }
}

Here is how you'd call it in your main :

public static void main(String[] args) {
    StudentDatabase database = new StudentDatabase();
    database.addStudent("Joe Schmo", 1101, 3.2);
    database.addStudent("Katie Katerson", 31415926, 3.7);
    database.addStudent("Watson TheBassetHound", 12345, 2.4);
    database.printDatabase();

    database.getStudentById(31415926);
    database.getStudentById2(31415926);
    database.getStudentById3(31415926);
}

Case 2: why don't you use java features ? You have lists, Maps... In your code you have StudentDatabase and StudentDatabaseLL objects ... I do suppose they look the same. Here is what I would do for your case:

public class Main {
    private static Map<String, List<Student>> studentListsMap = new HashMap<>();
    public static void main(String[] args) {
        List<Student> students;

        students = new ArrayList<>();
        students.add(new Student(11, "name1", 3.2));
        students.add(new Student(12, "name2", 4.335));
        students.add(new Student(13, "name3", 12.1));
        // will print all students
        printDatabase(students);
        System.out.println("*******");
        getStudentById(students, 11);

        studentListsMap.put("students", students);

        students = new ArrayList<>();
        students.add(new Student(11, "name1", 3.2));
        students.add(new Student(12, "name2", 4.335));
        students.add(new Student(13, "name3", 12.1));
        // will print all students
        printDatabase(students);
        System.out.println("*******");
        getStudentById(students, 12);
        getStudentById(students, 15);

        studentListsMap.put("studentsLL", students);
        
        // now you can also manage your two lists in the map
        System.out.println("from the map");
        printDatabase(studentListsMap.get("studentsLL"));
        System.out.println("*******");
        getStudentById(studentListsMap.get("studentsLL"), 13);
    }

    private static void printDatabase(List<Student> students) {
        for (Student student: students) {
            // here you'll use the toString from the Student POJO
            System.out.println(student);
        }
    }

    public static void getStudentById(List<Student> students, int idNumber) {
        System.out.println(
                students.stream().filter(student -> student.getIdNumber() == idNumber)
                        .findFirst().orElse(new Student(0, "John Doe", 0.0)));
    }

    public static void getStudentByIdV0(List<Student> students, int idNumber) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getIdNumber() == idNumber)
                System.out.println(students.get(i));

        }
    }
}

getStudentById is looking in your list of student if it founds the right student with the id you passed. If no result is found, it create a John Doe Student... It will print a student in all case

getStudentByIdV0, is checking student by idNumber and print it if found.

this code may look a little bit more complex but you do not have to reinvent the wheel.

Upvotes: 0

KhanhNhatt
KhanhNhatt

Reputation: 11

Here is how you should write the function and you should declare this func in the StudentDatabase class, as it is a function of it (according to OOP)

public void findStudentbyIndex(int i)
{
  Student std = database[i];
  System.out.println(std.getName() + ": (ID=" + std.getIdNumber() + "), (gpa=" + std.getGpa() + ")");
}

Sorry for the edit, i'm kinda new to this...

Upvotes: 1

Related Questions