Telephone directory using linked list [closed]
My program is about telephone directory I was use linked list I have problem in this code it doesn’t have syntax error but I can’t see the answer when I do search and list also I hope you to add update method
This is first class:
//-----------------------------------------------------------------------------------------
class tel
{
private static String name;
private static String address;
private static int phoneNo;
private tel next;
private tel prev;
public tel(){
}
public tel(String name1,String address1 ,int phoneNo1)
{
name = name1 ;
address = address1;
phoneNo = phoneNo1;
}
开发者_StackOverflow社区 public static String getName() {
return name;
}
public void setName(String name1) {
name = name1;
}
public static String getAddress() {
return address;
}
public void setAddress(String address1) {
address = address1;
}
public static int getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(int phoneNo1) {
phoneNo = phoneNo1;
}
public tel getNext() {
return next;
}
public void setNext(tel next1) {
next = next1;
}
public tel getPrev() {
return prev;
}
public void setPrev(tel prev1) {
prev = prev1;
}
public void display()
{
System.out.println("Name : " + getName() + " PhoneNo : " + getPhoneNo() + " Address : " + getAddress());
}
}
//-------------------------------------------------------------------------------------------------------------------
//2ed class
public class telList {
private tel first;
private tel last;
private tel cur ;
public telList()
{
first = null ;
last = null ;
}
public boolean isEmpty()
{return first == null; }
public tel getCur() {
return cur;
}
public void setCur(tel cur1) {
cur = cur1;
}
public void insert(String na,String addr ,int pho)
{
tel newtel = new tel(na,addr,pho);
if (isEmpty())
{
last = newtel;
first = newtel;
}
else
{
newtel.setNext(first);
first.setPrev(newtel);
first = newtel;
}
}
//////////////////////////////////
public tel search(String Key)
{
tel cur = first;
while(cur.getName() != Key)
{
if(cur.getNext()== null)
return null;
else
cur = cur.getNext();
}
return cur;
}
public void displaythelist()
{
tel cur = first;
while(cur != null)
{
System.out.println(cur.display());
cur = cur.getNext();
}
System.out.println(" ");
}
}
//3ed class
//-------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
public class telTest {
public static void main(String[] args)throws Exception {
Scanner input = new Scanner(System.in);
boolean quit = false;
do {
int menu = 0;
System.out.println("******************************");
System.out.println("Telephone Directory");
System.out.println();
System.out.println("1. Accept Data");
System.out.println("2. Search");
System.out.println("3. List of all persons");
System.out.println("4. Exit");
System.out.print("Please enter your choice: ");
menu = input.nextInt();
System.out.println();
tel t = new tel();
telList t1 = new telList();
switch (menu) {
case 1:
System.out.print("Enter Name: ");
String name = input.next();
t.setName(name);
System.out.print("Enter Address: ");
String address = input.next();
t.setAddress(address);
System.out.print("Enter Phone No: ");
int no = input.nextInt();
t.setPhoneNo(no);
t1. insert(t.getName(),t.getAddress(),t.getPhoneNo());
break ;
//...........................................................................
case 2:
System.out.print("Enter name to search information: ");
String n = input.next();
try {
t1.search(t.getName());
}
catch (Exception e) {
}
break;
//...........................................................................
case 3://list
try {
t1.displaythelist();
}
catch (Exception e) {
}
break;
//...........................................................................
case 4 ://exit
quit = true;
break;
//...........................................................................
default:
System.out.println("Invalid Entry!");
}
}while (!quit);
}
}
Replace
while(cur.getName() != Key)
with
while(!cur.getName().equals(Key))
You are comparing for object equality with !=
. You want to compare the characters of the string instead (two different strings with the same characters should compare as equal).
Are you sure they should be Static
??
private static String name;
private static String address;
private static int phoneNo;
I think it should be
private String name;
//... etc.
Edit : Also I think that
telList t1 = new telList();
on main()
should be outside the do
loop
Edit2 :
on main()
when you t1.insert(t.getName(),t.getAddress(),t.getPhoneNo());
on case 1:
You don't really use the object you created on main
so just do it like that :
case 1:
System.out.print("Enter Name: ");
String name = input.next();
//t.setName(name);
System.out.print("Enter Address: ");
String address = input.next();
//t.setAddress(address);
System.out.print("Enter Phone No: ");
int no = input.nextInt();
//t.setPhoneNo(no);
t1.insert(name,address,no);
From what I see you create a new object anyway in t1
But make sure you put telList t1 = new telList();
OUTSIDE of the do
loop because you re-instantiate it every time.
精彩评论