Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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();
}
}

public void insertAfter (String contactName, String contactPhoneNumber) node head = new node (contactName, cont actPhoneNumber, head);

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote