SlideShare a Scribd company logo
There is something wrong with my program.. (once I do a for view all this is the outcome in the
console...(carnea:Name: carnea, Address: 30 repeated..) why is this?
import java.util.Scanner;
public class AddressBook {
public static void main(String[] args) {
Tablea addressBook = new Tablea();
Scanner scanner = new Scanner(System.in);
String name, address;
String action = null;
boolean moreEntries = true;
while (moreEntries) {
System.out.println("/nMenu");
System.out.println("n.Add Entry");
System.out.println("d. Delete Entry");
System.out.println("u. Update Entry");
System.out.println("l. Search");
System.out.println("a. View All");
System.out.println("q. Exit");
action = scanner.nextLine();
// Insert a new entry into the address book
switch (action) {
case "n":
System.out.print("Enter a name: ");
name = scanner.nextLine();
System.out.print("Enter an address: ");
address = scanner.nextLine();
addressBook.insert(name, address);
System.out.println("New Contact has been added,");
break;
// Delete an entry from the address book
case "d":
System.out.println("Enter a name to delete: ");
name = scanner.nextLine();
boolean deleted = addressBook.delete(name);
if (deleted) {
System.out.println("Address deleted");
} else {
System.out.println("Name not found");
}
break;
// Update an entry in the address book
case "u":
System.out.print("Enter a name to update: ");
name = scanner.nextLine();
System.out.print("Enter a new address: ");
address = scanner.nextLine();
boolean updated = addressBook.update(name, address);
if (updated) {
System.out.println("Address updated");
} else {
System.out.println("Name not found");
}
break;
// Lookup an entry in the address book
case "l":
System.out.print("Enter a name to look up: ");
name = scanner.nextLine();
String result = addressBook.lookUp(name);
if (result != null) {
System.out.println("Address: " + result);
} else {
System.out.println("Name not found");
}
break;
// Display all entries in the address book
case "a":
System.out.println("All entries:");
addressBook.displayAll();
break;
// Quit the program
case"q":
System.out.println("Quitting...");
}
}
}
}
public class Node {
private String name;
private String address;
private Node next;
public Node(String name, String address) {
// add here ..
this.name = name;
this.address = address;
}
public String getName() {
// add here ..
return this.name;
}
public void setName(String name) {
// add here ..
this.name = name;
}
public String getAddress() {
// add here ..
return this.address;
}
public void setAddress(String address) {
// add here ..
this.address = address;
}
public Node getNext() {
// add here ..
return this.next;
}
public String toString() {
return "Name: "+ name +", Address:" +address;
}
public void setNext(Node node) {
// add here ..
this.next = next;
}
public void setAddress (Object newValue) {
this.address = address;
}
public void setName(Object newValue) {
this.name = name;
}
}
import java.util.Scanner;
public class Tablea {
private Node mark;
public Node getMark() {
return this.mark;
}
public void setMark(Node mark) {
this.mark = mark;
}
public boolean insert(String name, String address) {
Node newNode= new Node(name,address);
if (this.mark== null) {
this.mark = newNode;
}else {
newNode.setNext(this.mark.getNext());
this.mark.setNext(newNode);
}
return true;
}
public String lookUp(String name) {
Node current = this.mark;
while(current!=null) {
if (current.getName().equals(name)) {
return current.getAddress();
}
current = current.getNext();
}
return null;
}
public boolean delete(String name) {
Node current = this.mark;
Node prev = null;
while (current!= null) {
if (current.getName().equals(name)) {
if (prev == null) {
this.mark = current.getNext();
}else {
prev.setNext(current.getNext());
}
return true;
}
prev = current;
current = current.getNext();
}
return false;
}
public boolean update(String name, String newValue) {
Node current = this.mark;
while (current != null) {
if (current.getName().equals(name)) {
current.setAddress(newValue);
return true;
}
current = current.getNext();
}
return false;
}
public boolean markToStart() {
if (this.mark== null) {
return false;
}
this.mark = null;
return true;
}
public boolean advanceMark() {
if (this.mark== null) {
return false;
}
this.mark = this.mark.getNext();
return true;
}
public String keyAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getName();
}
public String valueAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getAddress();
}
public int displayAll() {
Node current = this.mark;
int count = 0;
while (current != null) {
System.out.println(current.getName()+":"+ current);
}
return count;
}
}
There is something wrong with my program-- (once I do a for view all t.pdf

More Related Content

PDF
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
PDF
How can I add multiple names and addresses- To the following code- Tab.pdf
PDF
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
PDF
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
PDF
How do you update an address according to this code- Currently- I fig.pdf
DOCX
java experiments and programs
PDF
Modify this code to change the underlying data structure to .pdf
PDF
So Far I have these two classes but I need help with my persontest c.pdf
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
How can I add multiple names and addresses- To the following code- Tab.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
How do you update an address according to this code- Currently- I fig.pdf
java experiments and programs
Modify this code to change the underlying data structure to .pdf
So Far I have these two classes but I need help with my persontest c.pdf

Similar to There is something wrong with my program-- (once I do a for view all t.pdf (10)

PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
[10] Write a Java application which first reads data from the phoneb.pdf
PDF
please help with java questionsJAVA CODEplease check my code and.pdf
PDF
In your Person classAdd a constant field to store the current yea.pdf
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
PDF
ObjectiveCreate a graphical database for a library IN JAVA. It sh.pdf
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
PDF
The hashtable youll be making will use Strings as the keys and Obje.pdf
PDF
Complete code in Java The hashtable you'll be making will use String.pdf
PDF
Can someone help me to fix the code please package dlist i.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
[10] Write a Java application which first reads data from the phoneb.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
In your Person classAdd a constant field to store the current yea.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
ObjectiveCreate a graphical database for a library IN JAVA. It sh.pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
The hashtable youll be making will use Strings as the keys and Obje.pdf
Complete code in Java The hashtable you'll be making will use String.pdf
Can someone help me to fix the code please package dlist i.pdf
Ad

More from aashienterprisesuk (20)

PDF
2 2- - Russia imposes restrictions on Czech exports as a retaliatory m.pdf
PDF
Your Role- Legal clerk for the firm of Carter- Carter- and Drake Audie.pdf
PDF
Which of the following statements is incorrect- The Solar System is lo.pdf
PDF
Type the program's output i-2 while i-16- j-3 while j--11- print (f{i}.pdf
PDF
the petty cash fund of the wee ones agency was established on may1- 20 (1).pdf
PDF
Simone founded her company using $150-000 of her own money- issuing he.pdf
PDF
Reflect on the following questions- What is your approach to understa.pdf
PDF
On January 2- 2024- Tobias Company began using straight-line depreciat.pdf
PDF
Match the descriptions to their appropriate plant tissue systems or st.pdf
PDF
If two individuals with a Tt genotype have an offspring- what are the.pdf
PDF
If A and B are independent events with P(A)-0-40 and P(B)-0-20- find P.pdf
PDF
Describe the Naturalistic ObservationDiscuss the strengths and weaknes.pdf
PDF
Budgeted sales for the first quarter for Shelley Company- a manufactur.pdf
PDF
Choose a company listed in the Main Market of Bursa Malaysia- Prepare.pdf
PDF
A country decided to adopt a fixed exchange rate policy and expansiona.pdf
PDF
Generally speaking- what is the direct function (purpose) of an action.pdf
PDF
During the Renaissance in Italy- many princes- noblemen- and wealthy b.pdf
PDF
For Ivanhoe Corporation- year end plan assets were $1-741-100- At the.pdf
PDF
A gene product called Smaug- is essential during the MZT to translatio.pdf
PDF
A modified rebuy is most likely to occur for-A- File foldersB- BroomsC.pdf
2 2- - Russia imposes restrictions on Czech exports as a retaliatory m.pdf
Your Role- Legal clerk for the firm of Carter- Carter- and Drake Audie.pdf
Which of the following statements is incorrect- The Solar System is lo.pdf
Type the program's output i-2 while i-16- j-3 while j--11- print (f{i}.pdf
the petty cash fund of the wee ones agency was established on may1- 20 (1).pdf
Simone founded her company using $150-000 of her own money- issuing he.pdf
Reflect on the following questions- What is your approach to understa.pdf
On January 2- 2024- Tobias Company began using straight-line depreciat.pdf
Match the descriptions to their appropriate plant tissue systems or st.pdf
If two individuals with a Tt genotype have an offspring- what are the.pdf
If A and B are independent events with P(A)-0-40 and P(B)-0-20- find P.pdf
Describe the Naturalistic ObservationDiscuss the strengths and weaknes.pdf
Budgeted sales for the first quarter for Shelley Company- a manufactur.pdf
Choose a company listed in the Main Market of Bursa Malaysia- Prepare.pdf
A country decided to adopt a fixed exchange rate policy and expansiona.pdf
Generally speaking- what is the direct function (purpose) of an action.pdf
During the Renaissance in Italy- many princes- noblemen- and wealthy b.pdf
For Ivanhoe Corporation- year end plan assets were $1-741-100- At the.pdf
A gene product called Smaug- is essential during the MZT to translatio.pdf
A modified rebuy is most likely to occur for-A- File foldersB- BroomsC.pdf
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Lesson notes of climatology university.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Presentation on HIE in infants and its manifestations
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
Lesson notes of climatology university.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Presentation on HIE in infants and its manifestations
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
FourierSeries-QuestionsWithAnswers(Part-A).pdf

There is something wrong with my program-- (once I do a for view all t.pdf

  • 1. There is something wrong with my program.. (once I do a for view all this is the outcome in the console...(carnea:Name: carnea, Address: 30 repeated..) why is this? import java.util.Scanner; public class AddressBook { public static void main(String[] args) { Tablea addressBook = new Tablea(); Scanner scanner = new Scanner(System.in); String name, address; String action = null; boolean moreEntries = true; while (moreEntries) { System.out.println("/nMenu"); System.out.println("n.Add Entry"); System.out.println("d. Delete Entry"); System.out.println("u. Update Entry"); System.out.println("l. Search"); System.out.println("a. View All"); System.out.println("q. Exit"); action = scanner.nextLine(); // Insert a new entry into the address book switch (action) { case "n": System.out.print("Enter a name: "); name = scanner.nextLine(); System.out.print("Enter an address: "); address = scanner.nextLine(); addressBook.insert(name, address); System.out.println("New Contact has been added,"); break; // Delete an entry from the address book case "d": System.out.println("Enter a name to delete: ");
  • 2. name = scanner.nextLine(); boolean deleted = addressBook.delete(name); if (deleted) { System.out.println("Address deleted"); } else { System.out.println("Name not found"); } break; // Update an entry in the address book case "u": System.out.print("Enter a name to update: "); name = scanner.nextLine(); System.out.print("Enter a new address: "); address = scanner.nextLine(); boolean updated = addressBook.update(name, address); if (updated) { System.out.println("Address updated"); } else { System.out.println("Name not found"); } break; // Lookup an entry in the address book case "l": System.out.print("Enter a name to look up: "); name = scanner.nextLine(); String result = addressBook.lookUp(name); if (result != null) { System.out.println("Address: " + result); } else { System.out.println("Name not found"); } break;
  • 3. // Display all entries in the address book case "a": System.out.println("All entries:"); addressBook.displayAll(); break; // Quit the program case"q": System.out.println("Quitting..."); } } } } public class Node { private String name; private String address; private Node next; public Node(String name, String address) { // add here .. this.name = name; this.address = address; } public String getName() { // add here .. return this.name; } public void setName(String name) { // add here .. this.name = name; } public String getAddress() { // add here .. return this.address; }
  • 4. public void setAddress(String address) { // add here .. this.address = address; } public Node getNext() { // add here .. return this.next; } public String toString() { return "Name: "+ name +", Address:" +address; } public void setNext(Node node) { // add here .. this.next = next; } public void setAddress (Object newValue) { this.address = address; } public void setName(Object newValue) { this.name = name; } } import java.util.Scanner; public class Tablea { private Node mark; public Node getMark() { return this.mark; } public void setMark(Node mark) { this.mark = mark; } public boolean insert(String name, String address) { Node newNode= new Node(name,address); if (this.mark== null) { this.mark = newNode;
  • 5. }else { newNode.setNext(this.mark.getNext()); this.mark.setNext(newNode); } return true; } public String lookUp(String name) { Node current = this.mark; while(current!=null) { if (current.getName().equals(name)) { return current.getAddress(); } current = current.getNext(); } return null; } public boolean delete(String name) { Node current = this.mark; Node prev = null; while (current!= null) { if (current.getName().equals(name)) { if (prev == null) { this.mark = current.getNext(); }else { prev.setNext(current.getNext()); } return true; } prev = current; current = current.getNext(); } return false; } public boolean update(String name, String newValue) { Node current = this.mark; while (current != null) { if (current.getName().equals(name)) { current.setAddress(newValue); return true; } current = current.getNext(); }
  • 6. return false; } public boolean markToStart() { if (this.mark== null) { return false; } this.mark = null; return true; } public boolean advanceMark() { if (this.mark== null) { return false; } this.mark = this.mark.getNext(); return true; } public String keyAtMark() { if (this.mark== null) { return null; } return this.mark.getName(); } public String valueAtMark() { if (this.mark== null) { return null; } return this.mark.getAddress(); } public int displayAll() { Node current = this.mark; int count = 0; while (current != null) { System.out.println(current.getName()+":"+ current); } return count; } }