我目前正在进行一个项目,我本来应该用eclipse在java中创建一个电话簿,我差不多完成了但是我的代码仍然是我的代码的一部分。 #39;理解。
以下代码示例是给我一些麻烦的部分,我的条件是(... contains(...))总是回答为true,即使这些联系人都没有在其名称中包含这部分字符串。此外,我还会显示手机的整个联系人,而我只想要匹配的那些。
我已尝试使用if(... contains(..)== true),但它也无效。
另一方面,寻找与ID匹配的第二部分完美地工作并且只回馈有关的" i"。使用相同的代码但适应我的字符串也不会产生任何好的结果。
public static void display(){
System.out.println("You selected display a contact, choose an option :");
System.out.println("1. by the first name");
System.out.println("2. by the last name");
System.out.println("3. by the ID");
System.out.println("4. by the phone number");
Scanner sc = new Scanner(System.in);
int str3 = sc.nextInt();
if(str3 == 1){
System.out.println("Please enter the first name of the contact");
String fname = sc.nextLine();
sc.nextLine();
for(int i = 0; i <= (phoneBook.size()-1) ; i++){
if(((phoneBook.get(i)).getFirstName()).toLowerCase().contains(fname.toLowerCase()) ){
displayContact(phoneBook.get(i));
}
}}
if(str3 == 2){
System.out.println("Please enter the last name of the contact");
String lname = sc.nextLine();
sc.nextLine();
for(int i = 0; i <= (phoneBook.size()-1) ; i++){
if(((phoneBook.get(i)).getLastName()).toLowerCase().contains(lname.toLowerCase())){
displayContact(phoneBook.get(i));
}
}
}
if(str3 == 3){
System.out.println("Please enter the ID of the contact");
int id = sc.nextInt();
for(int i = 0; i <= (phoneBook.size()-1) ; i++){
if(id == ((phoneBook.get(i)).getID())){
displayContact(phoneBook.get(i));
}
}
}
if(str3 == 4){
System.out.println("Please enter the phone number of the contact");
int phnb = sc.nextInt();
for(int i = 0; i <= (phoneBook.size()-1) ; i++){
if(phnb == phoneBook.get(i).getPhoneNb()){
displayContact(phoneBook.get(i));
}
}
}
System.out.println(" ");
System.out.println("If you want to go back at main menu press 1, else press 2 to quit.");
int str8 = sc.nextInt();
if(str8 == 1){
menu();
}
if(str8 == 2){
quit();
}
}
答案 0 :(得分:0)
我认为使用Scanner,首先读取nextInt,然后两次使用nextLine可能会导致空字符串,并且始终包含该字符串。您可以使用第二个nextLine,或者使用BufferedReader,它不易出错。
public static void display(){
System.out.println("You selected display a contact, choose an option :");
System.out.println("1. by the first name");
System.out.println("2. by the last name");
System.out.println("3. by the ID");
System.out.println("4. by the phone number");
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int str3 = Integer.parseInt(sc.readLine());
switch (str3) {
case 1:
System.out.println("Please enter the first name of the contact");
String fname = sc.readLine();
for (int i = 0; i < phoneBook.size(); i++) {
if (phoneBook.get(i).getFirstName().toLowerCase().contains(fname.toLowerCase())) {
displayContact(phoneBook.get(i));
}
}
break;
case 2:
System.out.println("Please enter the last name of the contact");
String lname = sc.readLine();
for (int i = 0; i < phoneBook.size(); i++){
if (phoneBook.get(i).getLastName().toLowerCase().contains(lname.toLowerCase())) {
displayContact(phoneBook.get(i));
}
}
break;
case 3:
System.out.println("Please enter the ID of the contact");
int id = Integer.parseInt(sc.readLine());
for (int i = 0; i < phoneBook.size(); i++) {
if (id == phoneBook.get(i)).getID()) {
displayContact(phoneBook.get(i));
}
}
break;
case 4:
System.out.println("Please enter the phone number of the contact");
int phnb = Integer.parseInt(sc.readLine());
for(int i = 0; i < phoneBook.size(); i++){
if(phnb == phoneBook.get(i).getPhoneNb()){
displayContact(phoneBook.get(i));
}
}
break;
}
System.out.println(" ");
System.out.println("If you want to go back at main menu press 1, else press 2 to quit.");
int str8 = Integer.parseInt(sc.readLine());
if(str8 == 1){
menu();
}
if(str8 == 2){
quit();
}
}
另外开关声明。