我一直在纠正我的代码中的错误,但我被困在两个不同的无法找到符号错误。一个是a1几行,(例如,addressbookmain.java中的第55行和几行上的Contact,(例如,addressbookmain.java中的第47行。如果有人能够解释为什么我会非常感激它!
代码:
contacts.java
public class contacts {
private String name;
private String housenum;
private String streetname;
private String town;
private String postcode;
private String address;
public Contact(String inname, String inhousenum, String instreetname, String intown, String inpostcode, String inaddress) {
this.name = inname;
this.housenum = inhousenum;
this.streetname = instreetname;
this.town = intown;
this.postcode = inpostcode;
this.address = inaddress
}
public address getaddress() {
return address
}
public String getname() {
return name;
}
public String gethousenum() {
return housenum;
}
public String getstreetname() {
return streetname;
}
public String gettown() {
return town;
}
public String getpostcode() {
return postcode;
}
public String getaddress() {
return name;
return housenum;
return streetname;
return town;
return postcode;
}
}
AddressBook.java
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class AddressBook {
public Contact[] contacts = new Contact[10];
private int count = 0;
private String fileName;
public AddressBook(String fileName) {
this.fileName = fileName;
}
public boolean add(Contact c) {
if(count > 9) {
return false;
}
contacts[count] = c;
count++;
return true;
}
public boolean remove(String deleteContact) {
for (int i = 0; i < count; i++){
if (contacts[i].getname() == deleteContact) {
contacts[i] = null;
contacts[i] = contacts[i-1];
}
}
count--;
return true;
}
public Contact search(String searchContact){
for (int i = 0; i < contacts.length; i++){
if (contacts[i].getname() == searchContact) {
return contacts[i];
}
return null;
}
}
public void output() {
System.out.println("Showing Contacts");
for(int i = 0; i < count; i++) {
System.out.println(contacts[i].getname() + " " + contacts[i].gethousenum() + " " + contacts[i].getstreetname() + " " + contacts[i].gettown() + " " + contacts[i].getpostcode());
}
}
public boolean load() {
try {
File fr = new File(fileName);
Scanner s = new Scanner(fr);
while(s.hasNextLine()) {
String oStore = s.nextLine();
String[] aStore = oStore.split(" : ");
contacts[count]=new Contact(aStore[0], aStore[1], aStore[2], aStore[3], aStore[4]);
count++;
}
s.close();
return true;
}
catch (Exception e){
return false;
}
}
public boolean save () {
try {
FileWriter fw = new FileWriter(fileName);
for(int i = 0; i < contacts.length; i++) {
fw.write(contacts[i].getname() + " " + contacts[i].gethousenum() + " " + contacts[i].getstreetname() + " " + contacts[i].gettown() + " " + contacts[i].getpostcode());
}
fw.close();
}
catch (Exception e) {
return false;
}
return true;
}
}
addressbookmain.java
import java.util.Scanner;
public class addressbookmain {
public static void main(String[] args) {
AddressBook a1 = new AddressBook("contacts.txt");
a1.load();
System.out.println("Welcome to my address book.");
System.out.println("");
displayMenu();
}
public static void displayMenu() {
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
System.out.println("\n");
System.out.println("What would you like to do? Enter 1, 2, 3, etc.");
System.out.println("1. Create a new address book");
System.out.println("2. Add a new contact");
System.out.println("3. Remove a contact");
System.out.println("4. Save current address book");
System.out.println("5. Show the current address book");
System.out.println("6. Search for a contact");
switch(choice) {
case 1:
System.out.println("/nWould you like to create a new address book?");
break;
case 2:
System.out.println("/nPlease enter details for the new contact");
System.out.print("Full name:");
String name = in.next();
System.out.print("House Number:");
String housenum = in.next();
System.out.print("Street Name:");
String streetname = in.next();
System.out.print("Town:");
String town = in.next();
System.out.print("Postcode:");
String postcode = in.next();
a1.add(new Contact(name, housenum, streetname, town, postcode));
System.out.println("Contact has been added.");
break;
case 3:
System.out.print("Enter a name to remove the address: ");
Scanner rs = new Scanner(System.in);
String deleteContact = rs.nextLine();
a1.remove(deleteContact);
break;
case 4:
System.out.println("/nAddress Book saved");
a1.save();
break;
case 5:
System.out.println("/nHere is the current address book");
a1.display();
break;
case 6:
System.out.print("Enter a name to search for the corresponding address: ");
Scanner ss = new Scanner(System.in);
String searchContact = ss.nextLine();
a1.search(searchContact);
break;
default:
System.out.println("Invalid input. Please enter a number between 1 and 6.");
}
}
}