Results 1 to 5 of 5
  1. #1
    kazit is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default Delete Student from Array Help

    Basically i have been trying to implement a student management system for an intro to java module i am taking on my course, i am a novice to java and have become a bit lost what to do next with me code any help greatly appreciated.

    The problem i have encountered is trying to delete a student from my student array and really cant figure how to do this.

    Dialogue Manager Class = Handles Main Menu
    Java Code:
    public class DialogueManager 
    {
       // Scanner for user input
       private Scanner scanner;
       
       //String to display the layout of the menu to the user
       private String menuString = "*****Main Menu*****\n\n" +
       							   "(1) - Add Student\n" +
       							   "(2) - Delete Student\n" +
       							   "(3) - Add Module\n" +
       							   "(4) - Assign Student to Module\n" +
       							   "(5) - Display Student Details\n" +
       							   "(6) - Exit Program\n";
       public DialogueManager()
       {
       	//Call the scanner class
        scanner = new Scanner(System.in);
       }
       
       //Enumerator for Yes, No user response
        private static enum Response
        {
            Yes,
            No
        }
        
        public Response prompt (String question)
        {
    		String input;
    
        	Response response = Response.No;
        	
        	System.out.print(question + " (Yes - y; No - n): ");
        	
        	input = scanner.next();
        	
        	if (input.equals("Y") || input.equals("y"))
        		{
        		response = Response.Yes;
        		}
        		
        	return response;
        }
          							   
       public int mainMenu()
       {
       	//integer to hold the users input
       	int choice;
       	Response answer; 
       		
       	System.out.println("Welcome to the Student Management System.\n");
       	
       	//answer = prompt("Would you like to see the Main Menu?");
       	
       	//while (answer == Response.Yes)
       	//{
       	//Print the main menu to the user
       	System.out.println(menuString);
       	
       	System.out.println("Please select an option!");
       	//}
       	
       	//Read the input from the user
       	choice = scanner.nextInt();
       	
       	//Return the user's choice
       	return choice;  	
       }   							  
    }
    StudentManager Class = Handles Main Program
    Java Code:
    public class StudentManager 
    {	
    	//Create Student Array
        Student[] stuArray = new Student[5];
        	
        //Create Module Array
        Module[] modArray = new Module[5];
        
        //Create a counter to keep a record of students
        private int studentCount = 0;
        
        //Create a counter to keep a record of modules
        private int moduleCount = 0;
        
        Scanner scanner = new Scanner(System.in);	
    
         public static void main(String[] args) 
        {
        	//Create a new instance of the DialogueManager class.
        	StudentManager game = new StudentManager();
        	
        	//Call the start method
        	game.Start();
        }
        
        private void Start()
        {
          int userOption = 0;
          
          //Create an instance of the dialogue manager
          DialogueManager dialogueMan = new DialogueManager();	
        	
          //Loop response for Yes
          while (userOption != 6)
          {
          	//Display the main menu
          	userOption = dialogueMan.mainMenu();
          	    
        	switch(userOption)
        	{
        		case 1: 
        			AddStudent();
        			break;
        		case 2:
        			DeleteStudent();
        			break;
        		case 3:
        			AddModule();
        			break;
        		case 4:
        			AssignModule();
        			break;
        		case 5:
        			DisplayDetails();
        			break;
        		case 6:
        			break;
        		default:
          			System.out.println("Invalid selection");
          			break;
        	}
          }
        }
        
        private void AddStudent()
        {
        	if (studentCount < 5)
        	{
        		//At least one student can be added
        		stuArray[studentCount] = new Student();
        		studentCount++;
        	}
        	else
        	{
        		System.out.println("No More Students Can Be Added To The Database!");
        	}
        	
        }
        
        private void DeleteStudent()
        {
        	studentCount = 5;
        	String studentName;
        	boolean found = false;
        	int loopCounter = 0;
        	
        	System.out.println("Enter the name of Student to Delete");
        	
        	studentName =  scanner.nextLine();
        	
        	while ((!found) && (loopCounter < studentCount))
        	{
        		found = stuArray[loopCounter].checkName(studentName);
        		
        		if(found == false)
        		{
        			loopCounter++;
        		}
        	}	
        }
        
        
        private void AddModule()
        {
        	if (moduleCount < 5)
        	{
        		//At least one module can be added
        		modArray[moduleCount] = new Module();
        		moduleCount++;
        	}
        	else
        	{
        		System.out.println("No More Modules Can Be Added To The Database!");
        	}
        } 
        
        private void AssignModule()
        {
        }
        
        private void DisplayDetails()
        {
        	System.out.println("Please Enter Student Name For Which Details Are Required:");
        	
        	System.out.println("Hello, I am a student and my name is xxx\n" +
        					   "I am in year xxx and enrolled on xxx.\n"+
        					   "This year I take the following modules:\n\n"+
        					   "xxx running in semester xxx\n"+
        					   "xxx running in semester xxx\n"+
        					   "xxx running in semester xxx\n");
        }
    }
    Java Code:
    Student Class = Handles Student
    	private String name;
    	private String date;
    	Module mod1, mod2, mod3;
    	
        public Student() 
        {
          Scanner scanner = new Scanner(System.in);	
          
          System.out.println("Please Enter a Student Name");
          
    	  name = scanner.nextLine();
    		
    	  System.out.println("Please enter student's enrolment date (DD/MM/YY)") ;
          
          date = scanner.nextLine();
        }
        
        public String getStudent()
        {
        	String result = "Student name: " + name + "\n";
        	return result;
        }
        
        public String getEnrolDate()
        {
        	String result = "Enrolment date: " + date + "\n";
        	return result;
        }
        
        public String getModule1()
        {
        	String result = "Module One: " + mod1 + "\n";
        	return result;
        }
        
        public String getModule2()
        {
        	String result = "Module Two: " + mod2 + "\n";
        	return result;
        }
        
        public String getModule3()
        {
        	String result = "Module Three: " + mod3 + "\n";
        	return result;
        }
        
        public String checkName()
        {	
        	return studentName;
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've not been able to compile the code above due to dependencies that I have no access to. A problem I see is the mixing up of user interface with program logic.

    For instance I think that it would be much better to create a more simple Student class like this one (Module stuff removed for simplicity and because I have no idea what a Module object is):
    Java Code:
    public class Student
    {
      private String name;
      private String enrollmentDate; // better would be to use util.Date not String
      
        public Student(String name, String enrollmentDate) 
        {
          this.name = name;
          this.enrollmentDate = enrollmentDate;
        }
        
        public String getName()
        {
          return name;
        }
        
        public String getEnrolDate()
        {
          return enrollmentDate;
        }
    }
    So the Student class would only be involved in storing and retrieving student information, that's it. It wouldn't worry about how to interact with the person using the program as that would be the province of another part of your program. The same would be true for the class that manages the Student array: it would only deal with adding students, removing students, sorting (if important), and things like that. It shouldn't have code that "talks" to the user or gets direct input from the user as that would be done in a separate part of the program.

    Just my 2 cents for what it's worth.

  3. #3
    kazit is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Okay thanks for your input on that i will work on re-arranging my program better as in regards to where user input is obtanied. Just to let you know i had included Module in teh student class as there is another module class in which students will have to be assigned three modules/subjects.

    So back to my original problem in my studentmanager class i tried to implement a deleteStudent method while also using a checkName method of student class handle search for student name could you help me with the logic of this as i am totally confused on how to implment this at the min.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what trouble are you having with your delete method? in an array, you need to shift everything over whenever you insert or delete and item, as well as modify any necessary variables.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default ArrayLists...

    Maybe it's a project requirement to use Arrays, but this would be a lot easier if you used (or could use) ArrayLists. It already has the methods to do the add, get, remove, etc.

    ArrayList (Java Platform SE 6)

    Luck,
    CJSL
    Last edited by CJSLMAN; 02-25-2009 at 01:40 PM. Reason: Clarified my comment
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 08:15 PM
  2. New java student needs help
    By cmizer in forum New To Java
    Replies: 9
    Last Post: 12-13-2008, 06:16 AM
  3. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 05:31 PM
  4. help student
    By jvasilj1 in forum New To Java
    Replies: 15
    Last Post: 02-02-2008, 08:23 AM
  5. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 01:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •