Results 1 to 11 of 11
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default Deleting a student

    Gotta love array stuff!
    Like many others, I have to make a program that can add, delete, and find a student (with ID#) from a list. In our case it's contained in a text file that we open via a provided FileUtil class. We also have a SortSearchUtil class that, obviously, sorts and searches.

    Edit: Has been solved, just had to tinker around with the length of myClass

    Java Code:
    public static Student [] deleteStudent(Student [] myClass, Scanner kb)
    	{
    		Student temp = readStudent(kb);
    		int res = SortSearchUtil.search(myClass, temp);
    		Student [] newArrayDel = new Student[myClass.length-1];
    		if (res==-1)
    		{
    			System.out.println("Not found");
    			return myClass;
    		}
    		for(int i = 0; i < res; i++)
     		{
     			newArrayDel[i]=myClass[i];
     		}
    		 for(int i = res+1; i <myClass.length; i++)
     		{
     			newArrayDel[i-1]=myClass[i];
     		}
     			
    		SortSearchUtil.sort(myClass);	
    		return newArrayDel;
    	}//end deleteStudent
    
    
    public static Student readStudent(Scanner kb){
    		String n;
    		int i;
    		System.out.print("Name (Last, First): ");
    		n = kb.nextLine();
    		System.out.print("ID#: ");
    		i = Integer.parseInt(kb.nextLine());
    		Student s = new Student (n,i);
    		return s;	
    	}//end readStudent
    Last edited by hiei_yasha; 02-11-2011 at 05:35 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    for(int i = 0; i < res; i++)
    {
        System.arraycopy(myClass, 0, myClass, 0, myClass.length-1);
    }
    for(int i = res; i <myClass.length-1; i++)
    {
        System.arraycopy(myClass, 0, newArrayDel, 0, myClass.length-1);
    }

    Don't guess: read the arraycopy() API docs to see what that method does.

    And work out what algorithm you are going to use to remove the entry (once you have found its index). I mean what, precisely and comprehensively, are the steps you are going to follow? Once you have figured them out in plain English you can write code with the number of loops that it actually needs.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'm not really sure what your problem is but your code can be simplified with just a single loop.
    Java Code:
    loop to newArrayDel length {
        if Student in myclass not the same as temp {
            copy Student in myClass into newArrayDel;
        }
    }
    Since the two arrays are different lengths and will be iterated at different rates you will need two indicies.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm thinking even fewer loops...

    Java Code:
    ......*.....
    |-  -| |- -|
       |    /
       |   /
    ...........

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yeah fewer loops for OP to write but the search and 2 calls to arraycopy have to loop over the array.

  6. #6
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Sorry, I wasn't aware that people had been posting. I've just been editing my OP lol

    What I have in my OP works for me, we technically haven't been taught arraycopy (generally if it hasn't been shown to us it's off limits) so I changed things around a little.

    Thanks for the helps! I really do appreciate your guys' time to help out a simpleton like me :)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    My guess would be that arraycopy() is there to be used, and that might well be optimised in some way.

    Yes, I'm assuming the index is known as that seems to be an assumption made by the way the question was posed. (ie by providing a utility method)

    But it's the OP's call...

    --------------

    Of course all of the elements of the newClass[] array need to be considered, not just the first newArrayDel.length of them. This adds a little complication to how you would write the body of the while loop you posted.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Now dealing with quite different code ... ;)

    So where do you get the null pointer exception? Post the whole stack trace if you can't understand it.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Where is NPE mentioned?

  10. #10
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    I had it at one point in my edit. I had been treating it as a "journal of my progress" kinda thing, but it's all good now. It compiles, no random exception things, I can add and delete and find whoever I want :)

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I can add and delete and find whoever I want
    Good to hear.

Similar Threads

  1. student need help
    By adamrain in forum New To Java
    Replies: 9
    Last Post: 11-29-2009, 12:10 AM
  2. Student--begginer--help!!!
    By AmandaIT in forum New To Java
    Replies: 6
    Last Post: 05-09-2008, 04:37 AM
  3. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 06:31 PM
  4. help student
    By jvasilj1 in forum New To Java
    Replies: 15
    Last Post: 02-02-2008, 09:23 AM
  5. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 02: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
  •