Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-24-2007, 12:52 PM   #1 (permalink)
Vatoreus
WAAAAAAAGH!
 
Vatoreus's Avatar
 
Join Date: Mar 2007
Location: Aviano, Italy
Posts: 3,113
-4 Internets
Send a message via MSN to Vatoreus
Java help.

Java Help

This is for my wife, needs help programming it correctly. It's a database that uses an array to add, list, search, and delete.

She's having trouble with being able to add input and when she runs it, it quits.

it's saved as a java file, using jGrasp to use it.

Any help would be greatly appreciated.
Attached Files
File Type: zip WindhamProject3Driver.zip (1.6 KB, 12 views)
Vatoreus is offline   Reply With Quote
Old 10-24-2007, 07:24 PM   #2 (permalink)
Hachima
Registered User
 
Join Date: Oct 2004
Posts: 1,654
the frist keyboard.nextLine() will be ignored when it follows a keyboard.nextInt()

You could request a dummy keyboard.nextLine() to clear the buffer of the newline.

The else if (choice >1 || choice <4 ) statement is incorrect and will always be true.

Last edited by Hachima : 10-24-2007 at 07:29 PM.
Hachima is offline   Reply With Quote
Old 10-25-2007, 04:07 AM   #3 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 785
-6 Internets
Since she's dealing with numbers, she might want to look into switch cases instead of all the if statements.
Would clean up the code some

Code:
int input; switch (input){ case 1: ..... case 2: ..... etc }
slitz is offline   Reply With Quote
Old 10-25-2007, 06:24 AM   #4 (permalink)
Vatoreus
WAAAAAAAGH!
 
Vatoreus's Avatar
 
Join Date: Mar 2007
Location: Aviano, Italy
Posts: 3,113
-4 Internets
Send a message via MSN to Vatoreus
Quote:
Originally Posted by Hachima View Post
The else if (choice >1 || choice <4 ) statement is incorrect and will always be true.
yea, this was changed, guess she didn't give me the copy of the code that we fixed that part on. The problem she was having is, as soon as she enters the number for the choice, it shows the next input option, then immediately skips to the loop sequence.
Vatoreus is offline   Reply With Quote
Old 10-25-2007, 01:03 PM   #5 (permalink)
Vatoreus
WAAAAAAAGH!
 
Vatoreus's Avatar
 
Join Date: Mar 2007
Location: Aviano, Italy
Posts: 3,113
-4 Internets
Send a message via MSN to Vatoreus
update, we got the inputs to work, but that's it. It isn't saving any of the inputs or anything.
Attached Files
File Type: zip WindhamProject3Driver.zip (1.7 KB, 12 views)
Vatoreus is offline   Reply With Quote
Old 10-25-2007, 03:46 PM   #6 (permalink)
Hachima
Registered User
 
Join Date: Oct 2004
Posts: 1,654
I haven't used jgrasp before but does it let you debug and step through the code? Set watches to see values as you step through the lines of code?

I use Eclipse which lets you do all that. If it doesn't let you debug your code I'd suggest switching to an IDE that does. It helps a lot of following your code and looking for errors.

Edit: The reason I say this is because instead of telling you exactly what is missing/wrong(it is pretty obvious why nothing prints when you try to list the data) it's better to learn how to step through your code and find your own errors. Either doing it my hand or using a debugger to step through the code line by line.

Last edited by Hachima : 10-25-2007 at 04:11 PM.
Hachima is offline   Reply With Quote
Old 10-26-2007, 12:18 AM   #7 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 785
-6 Internets
Quote:
Originally Posted by Vatoreus View Post
update, we got the inputs to work, but that's it. It isn't saving any of the inputs or anything.
No that's not the problem at all. She is saving the input, she isn't listing the input however.
First of all:
Code:
if (choice == 2) { int position = WindhamProject3Class.START_POSITION; next = assistant.getEntryAt(position); }
The only thing that this piece of code is doing is the following:
It saves a reference pointing at position 1 (ALWAYS) into the "next" variable, meaning, she's only saving the result but she's not doing anything with it.

What she probably do want to do however is to start with the START_POSITION and then iterate over "collection" and print out all objects that it includes. This can be done with the following code (using her other class):

Code:
if (choice == 2){ int size = assistant.getNumberOfEntries() + 1; for (int i=1;i < size ; i++){ System.out.println("[" +assistant.getEntryAt(i) + "]"); } }
Same with:
Code:
if (choice == 3) { System.out.print("Enter name to search for: "); String searchName = keyboard.nextLine(); searchName = keyboard.nextLine(); assistant.onList(searchName); }
She's doing everything right here, but she's not doing anything for the user to see that the program is actually working.
Since "assistant.onList(searchName)" returns a boolean, the only thing she needs to do is "System.out.println(assistant.onList(searchName));" and she'll get either a true or false on her search.

Code:
public String deleteEntry(String deleteName) { int[] b = new int[3]; int i = 0; int delete = 0; int elementToDelete = 5; while (i < entry.length && delete < b.length) { if (i == elementToDelete) { i++; } else { b[delete] = entry.length; i++; delete++; } return deleteName; } return null; }
This one isn't really working correctly and she's complicating the matter in fact. She got a string array with all her customers, why not look at it properly?

Code:
public String deleteEntry(String deleteName) { for (int i=0;i < entry.length;i++){ if (deleteName.equals(entry[i])){ entry[i] = null; rebuildArray(); return deleteName; } } return "User not found"; } private void rebuildArray() { // you just deleted one entry of your array, so you'll have to rebuild it with String[] newEntry = new String[entry.length-1]; int entryPointer = 0; int newEntryPointer = 0; while (entryPointer < entry.length){ if (entry[entryPointer] != null){ newEntry[newEntryPointer] = entry[entryPointer]; entryPointer++; newEntryPointer++; }else{ // you found the null object, jump to next element entryPointer++; } } entry = newEntry; }
Mind you, arrays are static and thus you will have to rebuild the array with the "rebuildArray" method I added (make it private).
She will have to decrease the size with one also (numberOFEntries--) and take the necesarry actions. Hope you get the idea.
Of course, this isn't very good looking or even good code, but I tried to make it as easy possible. She should however, unless it's part of the homework to use arrays, look into Vectors instead, which would make the task a lot easier.

Hope that makes sense....
If not, just reply and I'll try to be a bit more pedagogic.
PS! I would still advice her to use switch-statement instead of the if-else statement in the main class here, it would make the code a lot easier to read and this is a typical "switch-statement" case..

Last edited by slitz : 10-26-2007 at 12:52 AM.
slitz is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 10:50 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6