So the moral of the programming assignment below is that I need to write an addressbook w/ first, last, address, city/state, zip. I must use a switch statement but no ioexception or buffered reader. LiFiAddressBook file is separate from LastFirstChapter10 (but linked in the same box below). It seems to compile correctly but it switches between an error of cannot use a Boolean in a switch statement, skips over zip code input to go to choose an option to search, and etc. I don't have the exact error right now as I'm at work and have no way to compile to output the exact error. Just looking for suggestions or corrections.
"LiFiAddressBook.java class Instance variables: First Name (string) Last Name (string) Street Address (string) City State (string) Zip Code (string) addEntry method: Get input for variables above. See sample in example at bottom. search method: Receive ArrayList as argument Output Search Menu (see example at bottom) Utilize a switch and search ArrayList for field specified. Return index number if entry found or -1 if not found display method: Print results as shown in example at bottom."
LiFiAddressBook.java:33: error: incompatible types switch(isFound) { ^ required: int found: boolean 1 error
import java.util.ArrayList;
import java.util.Scanner;
public class LastFirstChapter10
{
public static void main(String [] args)
{
ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>();
String searchStr = null;
int field = 0;
Scanner stdIn = new Scanner(System.in);
int option = 0; ;
for(int count=0; count<1 ; count++)
{
LiFiAddressBook ab = new LiFiAddressBook();
System.out.println("Please Enter First Name: ");
ab.setFirstName(stdIn.next());
System.out.println("Please Enter Last Name: ");
ab.setLastName(stdIn.next());
System.out.println("Please Enter Street Address: ");
ab.setStreetAddress(stdIn.next());
System.out.println("Please Enter City, State: ");
ab.setCityState(stdIn.next());
System.out.println("Please Enter Zip Code: ");
ab.setZipCode(stdIn.next());
aBook.add(ab);
System.out.println("Enter Search Option:\n"+
"1. First Name\n"+
"2. Last Name\n"+
"3. Street Address\n"+
"4. City, State\n"+
"5. Zip Code\n");
option = stdIn.nextInt();
System.out.println("Enter your search string: ");
searchStr = stdIn.nextLine();
int foundIndex = LiFiAddressBook.search(aBook, searchStr,
field);
System.out.println();
if(foundIndex > -1)
{
LiFiAddressBook abc = aBook.get(foundIndex);
abc.display();
}
else {
System.out.println("No entry is found.");
} // end else
} //end for
} //end method
} //end class
import java.util.ArrayList;
public class LiFiAddressBook {
private String firstName;
private String lastName;
private String streetAddress;
private String cityState;
private String zipCode;
public LiFiAddressBook() {
}
public LiFiAddressBook(String firstName,String lastName, String streetAddress,
String cityState, String zipCode) {
this.firstName = firstName;
this.lastName = lastName;
this.streetAddress = streetAddress;
this.cityState = cityState;
this.zipCode = zipCode;
}
public static int search(ArrayList<LiFiAddressBook> aBook, String searchStr, int field)
{
if(aBook != null) {
boolean isFound = false;
for(int i=0; i < aBook.size(); i++) {
LiFiAddressBook ab = aBook.get(i);
switch(isFound) {
case 1:
//if(field == 1)
isFound = ab.getFirstName().equals(searchStr.trim());
case 2:
//if(field == 2)
isFound = ab.getLastName().equals(searchStr.trim());
case 3:
//if(field == 3)
isFound = ab.getStreetAddress().equals(searchStr.trim());
case 4:
//if(field == 4)
isFound = ab.getCityState().equals(searchStr.trim());
case 5:
//if(field == 5)
isFound = ab.getZipCode().equals(searchStr.trim());
}
if(isFound)
return i;
}
}
return -1;
}
public void display() {
System.out.println("First Name: "+this.firstName);
System.out.println("Last Name: "+this.lastName);
System.out.println("Street Address: "+this.streetAddress);
System.out.println("Zip Code: "+this.zipCode);
System.out.println("City, State: "+this.cityState);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCityState() {
return cityState;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
Aucun commentaire:
Enregistrer un commentaire