Welcome to Dream.In.Code
Become a Java Expert!

Join 150,429 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,077 people online right now. Registration is fast and FREE... Join Now!




Searching linked list! Need help fast

2 Pages V  1 2 >  
Reply to this topicStart new topic

Searching linked list! Need help fast, Hi i have this project due at 1:00 pm friday april 4th so don't bo

itsasher
4 Apr, 2008 - 08:59 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

Hi i am having problems using a search method in a linked list that i implemented myself, not the integrated one in java.
The program is a contact manage or address book.
The code compiles it just does not work.
the if else statements is where the problem is and i am not sure what is wrong with my logic.
the display method that is called for in the if else's should be functional and i am positive that is not the problem.
If you need me to post all of my code i will be more than happy to comply.

Note: this is due friday august 4th at 1:00pm so if past this time don't worry bout replying. Thank you for the help.
java

public void find_contact() {
/*
this is the method for finding a contact.
it has a menu so the user can choose what parameter they would like to use to search for a certain contact.
The following is the method used for finding a contact once a parameter has been selected:
1) a new node is set u called tempnode which is equal to the first node in the linked list
2) the user is prompted for the data( name, num, double) of the selected paramater by which they are searching
3) it stores this data and then starts comparing this data with the data in the first node
4) if the data in the node and the data of the temporary parameter are equal then the method calls on a printmethod
5) if they are not equal then the search moves on to the next node and this repeats until the two match up.
*/
char opt2;

do{
System.out.println("How would you like to search?");
System.out.println(" ");
System.out.println("F) By First Name");
System.out.println("L) By Last Name");
System.out.println("I) By ID Number");
System.out.println("G) By GPA");
System.out.println("H) By Hours");
System.out.println("P) By Phone Number");
System.out.println("S) By Street");
System.out.println("C) By City");
System.out.println("O) By State");
System.out.println("Z) By Zip Code");
System.out.println(" ");
System.out.println("Q) Exit Search");

opt2 = keyboard.next().charAt(0);
LinkedListNode tempnode = start;

if (opt2 == 'F') {
System.out.println("Enter the first name of the contact you wish to find.");
String tempfirst_name = keyboard.next();

if (tempnode.first_name ==tempfirst_name) {

this.show_results(tempnode);}
else {tempnode = tempnode.next;
System.out.println(" test");
return;
}
}
else if (opt2 == 'L') {
System.out.println("Enter the last name of the contact you wish to find.");
String templast_name = keyboard.next();
if (tempnode.last_name == templast_name) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'I') {
System.out.println("Enter the ID Number of the contact you wish to find.");
int tempid = keyboard.nextInt();
if (tempnode.id == tempid) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'G') {
System.out.println("Enter the GPA of the contact you wish to find.");
double tempgpa = keyboard.nextDouble();
if (tempnode.gpa == tempgpa) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'H') {
System.out.println("Enter the Hours of the contact you wish to find.");
int temphours = keyboard.nextInt();
if (tempnode.hours == temphours) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
/*else if (opt2 == 'P') {
System.out.println("Enter the Phone number of the contact you wish to find.");
String tempnumber = keyboard.next();
if (tempnode.number == tempnumber) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}*/
else if (opt2 == 'S') {
System.out.println("Enter the Street of the contact you wish to find.");
String tempaddress_num = keyboard.next();
if (tempnode.address_num == tempaddress_num) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'C') {
System.out.println("Enter the city of the contact you wish to find.");
String tempcity = keyboard.next();
if (tempnode.city == tempcity) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'O') {
System.out.println("Enter the state of the contact you wish to find.");
String tempstate = keyboard.next();
if (tempnode.state == tempstate) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'Z') {
System.out.println("Enter the zip code of the contact you wish to find.");
int tempzip_code = keyboard.nextInt();
if (tempnode.zip_code == tempzip_code) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'F') {

}


}
while (opt2 != 'Q');




}

*edit: I saw your attempt to use code tags, so thanks for that. It is... code.gif

This post has been edited by Martyr2: 4 Apr, 2008 - 09:21 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:29 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
First of all you need to compare strings with the equals function. This is for your "string" variables. For instance...

java

if (tempnode.first_name.equals(tempfirst_name)) {


Please post your entire code if you can get the chance so that I can spin through it on a test run and make any needed changes. Plus what time zone are you in?
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:32 AM
Post #3

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

java

import java.util.Scanner;
import java.io.*;

public class project0a{

private LinkedListNode start;
Scanner keyboard = new Scanner(System.in);//sets up keyboard
LinkedList List = new LinkedList();//sets up linked list called List

public static void main(String[] args) {
/*
This sets up the loop for the entire program so that it always returns back to the menu
untill the user asks to exit the program
*/
project0a loopthrough = new project0a();
loopthrough.menu();

}

public void menu() {

int op;
/*
This is the main menu for the program and it leads to the areas specified.
In the case of find contact and edit a contact both of these have their own
specific menus integrated.
find contact also holds the method for deleting a contact
*/
do{
System.out.println("_________Adress Book_________");
System.out.println(" 1) Enter a new contact");
System.out.println(" 2) Find a contact");
System.out.println(" 3) Edit a contact");
System.out.println(" 4) Display all contacts");
System.out.println(" 5) Quit");
System.out.println("_____________________________");


op = keyboard.nextInt();
/*
these are the menu options that lead to their respective methods
*/
if (op == 1) {
this.new_contact();
}
else if (op == 2) {
List.find_contact();
}
else if (op == 3) {
this.edit_contact();
}
else if (op == 4) {
this.display_contacts();
}
}while (op != 5);
}

/*
the following is the node for the linked list. It contains all of the necessary information for the node
to be able to store a contact: all of the strings, ints, doubles and it properly sets them up like the
Contact class in previous labs.
*/

public class LinkedListNode { //setsup the node for the linked list
int value;
String first_name;
String last_name;
int id;
double gpa;
int hours;
PhoneNumber number;
LinkedListNode next;
String address_num;
String city;
String state;
int zip_code;

LinkedListNode() {
address_num = " ";
city = " ";
state = " ";
zip_code = 0;
first_name = " ";
last_name = " ";
id = 0;
gpa = 0;
hours = 0;
number = new PhoneNumber();
next = null;//the next node is empty untill something is added
}

}
public class PhoneNumber{//sets up the values of the phone number to be refrenced to later
int area, prefix, suffix;
public PhoneNumber(){
area = 0;
prefix = 0;
suffix = 0;
}
}
/*
The following method is for editing and deleting contacts.
*/
public void edit_contact() {
//put another search method here then go to the menu below
char opt3;
/*
a menu for editing and deleting contacts follows
*/
do{
System.out.println("What would you like to edit?");
System.out.println(" ");
System.out.println("D) Delete a contact");
System.out.println("F) Edit First Name");
System.out.println("L) Edit Last Name");
System.out.println("I) Edit ID Number");
System.out.println("G) Edit GPA");
System.out.println("H) Edit Hours");
System.out.println("P) Edit Phone Number");
System.out.println("S) Edit Street");
System.out.println("C) Edit City");
System.out.println("O) Edit State");
System.out.println("Z) Edit Zip Code");
System.out.println(" ");
System.out.println("Q) Exit Edit");

opt3 = keyboard.next().charAt(0);
LinkedListNode editnode = start; //initiates a temporary node so that editing can be done

if (opt3 == 'D') {

}
else if (opt3 == 'F') {// if F asks the user to input the new name of the contact which is then stored in the node and it overwrites the old name.
System.out.println("Enter the new First name of the contact");
editnode.first_name = keyboard.next();

}

else if (opt3 == 'L') {// if L asks the user for the new last name of the contact which is then stored in the node and it overwrites the old name
System.out.println("Enter the new Last name of the contact");
editnode.last_name = keyboard.next();

}

else if (opt3 == 'I') {// asks for a new id number and overwrites the old one
System.out.println("Enter Student ID number");
System.out.println("900_ _ _ _ _ _ _");
editnode.id = keyboard.nextInt();

}

else if (opt3 == 'G') {//asks for a new gpa or hours and overwrites the old data
char opt;
System.out.println("G) Enter the GPA for this student");
System.out.println("H) Enter the number of completed credit hours for the student");
opt = keyboard.next().charAt(0);
if (opt == 'G') {//if (GPA !=0) then search.gpa
System.out.println("GPA: ");
editnode.gpa =keyboard.nextDouble();
//this.PhoneNumber();
}
else if (opt == 'H') {
System.out.println("Credit Hours: ");
editnode.hours = keyboard.nextInt();
//this.PhoneNumber();
}
else {
return;
}
}

else if (opt3 == 'P') {// asks for the new phone number and overwrites the old one
System.out.println("Enter the phone number of this contact");
System.out.println("(###)-###-####:");
int area = keyboard.nextInt();
int prefix = keyboard.nextInt();
int suffix = keyboard.nextInt();
PhoneNumber phone = editnode.number;
phone.area = area;
phone.prefix = prefix;
phone.suffix = suffix;

}

else if (opt3 == 'S') {//asks for the new street address and overwrites the old data
System.out.println("Enter the street address of the contact");
editnode.address_num = keyboard.next();

}

else if (opt3 == 'C') {//asks for the new city and overwrites the old one
System.out.println("Enter the city of the contacts address");
editnode.city = keyboard.next();
}

else if (opt3 == 'O') {//asks for the new state and overwrites the old one
System.out.println("Enter the state of the contacts address");
editnode.state = keyboard.next();
}

else if (opt3 == 'Z') {//asks for the new zip and overwrites the old one
System.out.println("Enter the zip code of the contacts address");
editnode.zip_code = keyboard.nextInt();

}
else if (opt3 == 'Q') {System.out.println("Returning to menu....");}

} while (opt3 !='Q' );
}
/*
the following class is for the linked list
it also contains the method for searching for a contact, i. e. searching for a node with certain parameters
*/
public class LinkedList {
//initiates the linked list
LinkedListNode current = start;
LinkedList() {

}

public void find_contact() {
/*
this is the method for finding a contact.
it has a menu so the user can choose what parameter they would like to use to search for a certain contact.
The following is the method used for finding a contact once a parameter has been selected:
1) a new node is set u called tempnode which is equal to the first node in the linked list
2) the user is prompted for the data( name, num, double) of the selected paramater by which they are searching
3) it stores this data and then starts comparing this data with the data in the first node
4) if the data in the node and the data of the temporary parameter are equal then the method calls on a printmethod
5) if they are not equal then the search moves on to the next node and this repeats until the two match up.
*/
char opt2;

do{
System.out.println("How would you like to search?");
System.out.println(" ");
System.out.println("F) By First Name");
System.out.println("L) By Last Name");
System.out.println("I) By ID Number");
System.out.println("G) By GPA");
System.out.println("H) By Hours");
System.out.println("P) By Phone Number");
System.out.println("S) By Street");
System.out.println("C) By City");
System.out.println("O) By State");
System.out.println("Z) By Zip Code");
System.out.println(" ");
System.out.println("Q) Exit Search");

opt2 = keyboard.next().charAt(0);
LinkedListNode tempnode = start;

if (opt2 == 'F') {
System.out.println("Enter the first name of the contact you wish to find.");
String tempfirst_name = keyboard.next();
System.out.println("hi ");

if (tempnode.first_name == tempfirst_name) {
System.out.println(" test");

System.out.format("\n CONTACT \n");
System.out.format("Name: %s %s \n",tempnode.first_name,tempnode.last_name);


this.show_results(tempnode);}
else {tempnode = tempnode.next;
System.out.println("test");
return;
}
}
else if (opt2 == 'L') {
System.out.println("Enter the last name of the contact you wish to find.");
String templast_name = keyboard.next();
if (tempnode.last_name == templast_name) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'I') {
System.out.println("Enter the ID Number of the contact you wish to find.");
int tempid = keyboard.nextInt();
if (tempnode.id == tempid) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'G') {
System.out.println("Enter the GPA of the contact you wish to find.");
double tempgpa = keyboard.nextDouble();
if (tempnode.gpa == tempgpa) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'H') {
System.out.println("Enter the Hours of the contact you wish to find.");
int temphours = keyboard.nextInt();
if (tempnode.hours == temphours) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
/*else if (opt2 == 'P') {
System.out.println("Enter the Phone number of the contact you wish to find.");
String tempnumber = keyboard.next();
if (tempnode.number == tempnumber) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}*/
else if (opt2 == 'S') {
System.out.println("Enter the Street of the contact you wish to find.");
String tempaddress_num = keyboard.next();
if (tempnode.address_num == tempaddress_num) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'C') {
System.out.println("Enter the city of the contact you wish to find.");
String tempcity = keyboard.next();
if (tempnode.city == tempcity) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'O') {
System.out.println("Enter the state of the contact you wish to find.");
String tempstate = keyboard.next();
if (tempnode.state == tempstate) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'Z') {
System.out.println("Enter the zip code of the contact you wish to find.");
int tempzip_code = keyboard.nextInt();
if (tempnode.zip_code == tempzip_code) {
this.show_results(tempnode);}
else {tempnode = tempnode.next;}
}
else if (opt2 == 'F') {

}


}
while (opt2 != 'Q');




}
/*
the following method is used by the above find method. when a contact is found the find method tells the program to come
here to print out the node that it found. System.out.format is used to print out the nodes
*/
public void show_results(LinkedListNode tempnode){
System.out.format("\n CONTACT \n");
System.out.format("Name: %s %s \n",tempnode.first_name,tempnode.last_name);
System.out.format("ID Number: 900%6d\n",tempnode.id);
//System.out.format("%d\n",tempnode.gpa,tempnode.hours);
System.out.format("Phone Number:(%3d) %3d %4d\n ",tempnode.number.area,tempnode.number.prefix,tempnode.number.suffix);
System.out.format(" Address \n");
System.out.format("Street: %1s\n",tempnode.address_num);
System.out.format("City: %1s\n",tempnode.city);
System.out.format("State: %1s\n",tempnode.state);
System.out.format("Zip: %6d\n",tempnode.zip_code);
}

}
/*
the following method is for the addition of a new node (Contact)
the program adds a new node by first asking if start is empty. if start is not empty then start becomes a new node called contact.
if the current contact contains information then it is the first contact.
if the current contact already has information then the new current one becomes a new contact
*/
public void add (LinkedListNode Contact) {
if(start == null){
start = Contact;
}
else{
LinkedListNode current = start;
while(current.next!= null){
current = current.next;
}
current.next = Contact;
}
}

public class address {//address class
String address_num;
String city;
String state;
String zip_code;



}

/*the next method is for the addition of a new contact in labs 5 and 7 i used different methods to get each bit of information
but i could not get this old way to work. i had to combine it all because when i create a new Contact node it has to be accessable
by all the methods. therefore it had to be combined into one method which is as follows.
it stores each bit of data into the linked list.

*/
public void new_contact() {
LinkedListNode Contact = new LinkedListNode();
System.out.println("Enter the First name of the contact");
Contact.first_name = keyboard.next();

System.out.println("Enter the Last name of the contact");
Contact.last_name = keyboard.next();


System.out.println("Enter Student ID number");
System.out.println("900_ _ _ _ _ _ _");
Contact.id = keyboard.nextInt();


char opt;
System.out.println("G) Enter the GPA for this student");
System.out.println("H) Enter the number of completed credit hours for the student");
opt = keyboard.next().charAt(0);
if (opt == 'G') {//if (GPA !=0) then search.gpa
System.out.println("GPA: ");
Contact.gpa =keyboard.nextDouble();
//this.PhoneNumber();
}
else if (opt == 'H') {
System.out.println("Credit Hours: ");
Contact.hours = keyboard.nextInt();
//this.PhoneNumber();
}
else {
return;
}

System.out.println("Enter the phone number of this contact");
System.out.println("(###)-###-####:");
int area = keyboard.nextInt();
int prefix = keyboard.nextInt();
int suffix = keyboard.nextInt();
PhoneNumber phone = Contact.number;
phone.area = area;
phone.prefix = prefix;
phone.suffix = suffix;


System.out.println("Enter the street address of the contact");
Contact.address_num = keyboard.next();
System.out.println("Enter the city of the contacts address");
Contact.city = keyboard.next();
System.out.println("Enter the state of the contacts address");
Contact.state = keyboard.next();
System.out.println("Enter the zip code of the contacts address");
Contact.zip_code = keyboard.nextInt();

this.add(Contact);
}



/*
the following method is for displaying the contacts in the node.
it calls on the node while the node is not empty. after each displayed contact it checks to see
weather the next dode is empty and if it is not then it prints out the contact
System.out.format is used for simplicity and repeatability
*/

public void display_contacts() {//displays numbers in reverse order
LinkedListNode temp= start;

while (temp!=null) {

System.out.format("\n CONTACT \n");
System.out.format("Name: %s %s \n",temp.first_name,temp.last_name);
System.out.format("ID Number: 900%6d\n",temp.id);
//System.out.format("%d\n",temp.gpa,temp.hours);
System.out.format("Phone Number:(%3d) %3d %4d\n ",temp.number.area,temp.number.prefix,temp.number.suffix);
System.out.format(" Address \n");
System.out.format("Street: %1s\n",temp.address_num);
System.out.format("City: %1s\n",temp.city);
System.out.format("State: %1s\n",temp.state);
System.out.format("Zip: %6d\n",temp.zip_code);




temp = temp.next;
}
}

}

*edit: Again, please use the code tags like this.... code.gif

This post has been edited by Martyr2: 4 Apr, 2008 - 09:34 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:38 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
What time zone are you in?
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:39 AM
Post #5

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

Sorry about the double error...

I am in MDT and it is 11:36 AM

thanks
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:41 AM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Ok, I am working on this, give me about 20 minutes or so.

smile.gif
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 09:43 AM
Post #7

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

QUOTE(Martyr2 @ 4 Apr, 2008 - 10:41 AM) *

Ok, I am working on this, give me about 20 minutes or so.

smile.gif


Awesome! Thank you so much i really appreciate the help.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 10:14 AM
Post #8

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Ok well I got your first name searching fine and now it is just a matter of replicating the search code in that for each of the other search criteria. You haven't been making this easy for me.

wink2.gif
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 10:18 AM
Post #9

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

Great!
So did you use the syntax that you described earlier with the .equals?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 10:31 AM
Post #10

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yes and also you were missing the actual looping mechanism for moving from node to node. I have it now search on all options, but there is a collection problem I am still addressing so it will be a few more minutes.

smile.gif
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 10:34 AM
Post #11

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

QUOTE(Martyr2 @ 4 Apr, 2008 - 11:31 AM) *

Yes and also you were missing the actual looping mechanism for moving from node to node. I have it now search on all options, but there is a collection problem I am still addressing so it will be a few more minutes.

smile.gif


Can't thank you enough!
User is offlineProfile CardPM
+Quote Post

itsasher
RE: Searching Linked List! Need Help Fast
4 Apr, 2008 - 10:55 AM
Post #12

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 8

Hey!... don't mean to bug but if you can i'm still gonna have to script it so any extra bit of time would be doubly awesome.

Thanks Steven

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month