1. Member
Join Date
Jan 2011
Posts
34
Rep Power
0

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)
{
int res = SortSearchUtil.search(myClass, temp);
Student [] newArrayDel = new Student[myClass.length-1];
if (res==-1)
{
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

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;
Last edited by hiei_yasha; 02-11-2011 at 04:35 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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. 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
I'm thinking even fewer loops...

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

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

6. Member
Join Date
Jan 2011
Posts
34
Rep Power
0
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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. Where is NPE mentioned?

10. Member
Join Date
Jan 2011
Posts
34
Rep Power
0
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
I can add and delete and find whoever I want
Good to hear.

Posting Permissions

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