|
| | #1 (permalink) |
| WAAAAAAAGH! Join Date: Mar 2007 Location: Mt. Pleasant, TX
Posts: 3,123
| 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. |
| | |
| | #2 (permalink) |
| Registered User Join Date: Oct 2004
Posts: 1,695
| 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. |
| | |
| | #3 (permalink) |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| 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:
|
| | |
| | #4 (permalink) |
| WAAAAAAAGH! Join Date: Mar 2007 Location: Mt. Pleasant, TX
Posts: 3,123
| 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. |
| | |
| | #6 (permalink) |
| Registered User Join Date: Oct 2004
Posts: 1,695
| 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. |
| | |
| | #7 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Quote:
First of all: Code:
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:
Code:
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:
Code:
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. | |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |