I am traying to creat a linked list that received and output three contacts; but
ID: 3873078 • Letter: I
Question
I am traying to creat a linked list that received and output three contacts; but Im getting this error.. need help please
this the code
package linkedlist;
public class ContactNode {
static class node{
public String contactName;
public String contactPhoneNumber;
public node nextNodePtr;
public node(String contactName, String contactPhoneNumber,node c ) {
this.contactName = contactName;
this.contactPhoneNumber = contactPhoneNumber;
//this.nextNodePtr = nextNodePtr;
}
}
node head;
public void insertAfter(String contactName, String contactPhoneNumber) {
node head = new node(contactName, contactPhoneNumber,head);
}
public void printContactNode() {
node list = head;
System.out.println("CONTACT LIST");
while (head != null) {
System.out.println("Name: " + list.contactName + " Phone number: " + list.contactPhoneNumber);
list = list.nextNodePtr;
}
}
}
package linkedlist;
import java. util . Scanner;
public class ContactList {
public static void main(String[] arg){
System.out.println("");
Scanner sc = new Scanner(System.in);
ContactNode list= list = new ContactNode();
for (int x=1; x<4;x++){
System.out.println("Person "+x+" Enter name:");
String name = sc.nextLine();
System.out.println("Enter phone number:");
String phoneNumber= sc.nextLine();
System.out.println("You entered: "+name+","+phoneNumber);
list.insertAfter(name, phoneNumber);
}
list.printContactNode();
}
}
Explanation / Answer
package linkedlist;
public class ContactNode {
static class node{
public String contactName;
public String contactPhoneNumber;
public node nextNodePtr;
public node(String contactName, String contactPhoneNumber,node c ) {
this.contactName = contactName;
this.contactPhoneNumber = contactPhoneNumber;
//this.nextNodePtr = nextNodePtr;
}
}
node head;
public void insertAfter(String contactName, String contactPhoneNumber) {
node h = new node(contactName, contactPhoneNumber,head);
}
public void printContactNode() {
node list = head;
System.out.println("CONTACT LIST");
while (head != null) {
System.out.println("Name: " + list.contactName + " Phone number: " + list.contactPhoneNumber);
list = list.nextNodePtr;
}
}
}
package linkedlist;
import java. util . Scanner;
public class ContactList {
public static void main(String[] arg){
System.out.println("");
Scanner sc = new Scanner(System.in);
ContactNode list= list = new ContactNode();
for (int x=1; x<4;x++){
System.out.println("Person "+x+" Enter name:");
String name = sc.nextLine();
System.out.println("Enter phone number:");
String phoneNumber= sc.nextLine();
System.out.println("You entered: "+name+","+phoneNumber);
list.insertAfter(name, phoneNumber);
}
list.printContactNode();
}
}
============
Take this code its working now, Issue was same variable name I changed it to h and it worked fine
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.