Hello.

I am doing a project for a database that takes in student details and allows users to display their records.

My problem is this:
I have created a class called student (below coding) and created a menu class that displays the menu (2nd coding)
I seem to be stuck on the part of inputting information and using the menu class i want to display the information.

Can someone please help me out on this one?

package CW;
import java.io.*;

public class Student extends Person
{
	
		// atribute definitions
		
		private static int  studentNo;
		private int  yearOfstudy;
		
		Person Student[] = new Person[5];
		
		public Student(String fn,String ln,String add,String t,String dob, String phno,int sn,int ys)//constructor with two parameters student no,year of study
		{
			super(fn,ln,add,t,dob,phno);
			setStudentno(sn);
			setYearofStudy(ys);
			
		}
		
		
			
		
		//setter
		public void setStudentno( int sn)
		{
			studentNo= sn;
		}
		
		public void setYearofStudy(int ys)
		{
			yearOfstudy = ys;
		}
		
	
		//getter
		public int getStudentno()
		{
			return studentNo;
		}
		public int getYearofStudy()
		{
			return yearOfstudy;
		}
		
		public String tellaAoutself()
		{
			String detailsOfStudent =  getTitle()+  getFirstname()+  getLastname() + getDateofBirth()+ 
			getYearofStudy()+ getStudentno()+getPhoneno();
			
			return detailsOfStudent ;
		}
	
		public static void main(String[]args )
		{
			
			
			try
			
  			{
   
   				BufferedReader reader;
   				reader = new BufferedReader(new InputStreamReader(System.in));
   				String studentNo = reader.readLine(); //returns a String from the keyboard (when you press return)
   	 			String yearOfstudy = reader.readLine();
   	 			
   	 		
	 			
  			}
  			catch (IOException e){
  				
   			System.out.println(e);
  		}
  
  			
   
  		}
  
	 	
   	 	
	
		
		
}
package CW;
import java.io.*;
public class UniversityAdmin
{
	
	public static void main(String args[])
	throws IOException
	{
	
	//Allow a user to select operations using a text menu
		char mainMenu;
	
		
		do
		{
			System.out.println("\nWELCOME TO MIDDLESEX  UNIVERSITY DATABASE");
			System.out.println("\nPlease Make A Choice From The Menu Below");
			System.out.println(" Type 1. Display all Persons in the University");
			System.out.println(" Type 2. Display Student details");
			System.out.println(" Type 3. Display Lecturer details");
			System.out.println(" Type 4. Display all modules");
			System.out.println(" Type 5. Search for a Person by Surname");
			System.out.println(" Type 6. Dynamically add a new Student");
			System.out.println(" Type 7. Load from file");
			System.out.println(" Type 8  To end the program ");
			System.out.println(" Type Your Choice Here:");
		
		
			mainMenu=(char) System.in.read();
			
			
		}
		
		while (mainMenu<'1'||mainMenu>'8');
		System.out.println("\n");
		switch(mainMenu)
		{
			case '1':
			
			
			break; 	
		
			
		}
		
		
		
			
			
			
		
		
	}
}

i would apreciate ne help
Thx

Dani AI

Generated

The code shows the right idea but a few structural issues are stopping input and display from working reliably. Two quick fixes first: make per-student fields instance (not static) and keep the collection of students in your "admin" class instead of inside the Student class. Also avoid low-level single-byte reads for menu input; use a line-oriented reader so you can parse numbers and strings cleanly.

A simple, clear pattern to follow: a plain Student data class (only instance fields and a useful toString), and a separate UniversityAdmin that holds a List<Student>, reads user input with Scanner, builds Student objects, and prints them. Example Student (illustrative):

public class Student {
    private String firstName;
    private String lastName;
    private int studentNo;
    private int yearOfStudy;

    public Student(String firstName, String lastName, int studentNo, int yearOfStudy) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentNo = studentNo;
        this.yearOfStudy = yearOfStudy;
    }

    @Override
    public String toString() {
        return firstName + " " + lastName + " (id=" + studentNo + ", year=" + yearOfStudy + ")";
    }
}

And the controller pattern to add/list students from the console:

Scanner sc = new Scanner(System.in);
List<Student> students = new ArrayList<>();
while (true) {
    System.out.println("1) Add  2) List  3) Exit");
    String choice = sc.nextLine().trim();
    if ("1".equals(choice)) {
        System.out.print("First name: ");
        String fn = sc.nextLine();
        System.out.print("Last name: ");
        String ln = sc.nextLine();
        int id = Integer.parseInt(sc.nextLine().trim());
        int year = Integer.parseInt(sc.nextLine().trim());
        students.add(new Student(fn, ln, id, year));
    } else if ("2".equals(choice)) {
        students.forEach(System.out::println);
    } else break;
}
sc.close();

Notes and troubleshooting: wrap Integer.parseInt calls in try/catch to handle bad input; avoid static fields for per-instance data or every Student will show the same id; keep UI code out of model classes so tests and persistence are easier. For persistent storage, was pointing you in the right direction — when ready, learn JDBC from the official tutorial: JDBC Basics.

Recommended Answers

All 4 Replies

You need to learn about file operations.

Here is a basic structure of what you need:

File f = new File("file.txt");
BufferedReader br = new BufferedReader(new FileReader(f));

String line = "";
while ( (line = br.readLine()) != null)
{
}

My bad, you were talking about databases. Then create a connection and then look through querying the database.

My bad, you were talking about databases. Then create a connection and then look through querying the database.

What do u mean by creating a connection, could you xplain abit more cuz i am new to this stuff...

If you're new then you need to do some research. Google JDBC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.