2017-08-23 7 views
0

携帯電話の連絡先リストをシミュレートするためのコーディング練習をしていますが、既存の連絡先を検索してシステム入力から新しい連絡先を追加することはできますが、コンタクト用のすべてのクラスの まず:ここ Java:Scanner and ArrayList

が私のコードである(私は本当に、あなたの助けをいただければ幸いです事前に感謝そう!):なぜ知っている私は保持するためにMobilePhoneクラスを作成した後
public class Contacts { 

private String contactName; 
private String phoneNumber; 

public Contacts(){ 

} 

public Contacts(String contactName, String phoneNumber) { 
    this.contactName = contactName; 
    this.phoneNumber = phoneNumber; 
} 

public String getContactName() { 
    return contactName; 
} 

public String getPhoneNumber() { 
    return phoneNumber; 
} 
} 

addContact()メソッドとsearchContactByName()メソッドを使用してArrayListに連絡します。

01 mainメソッドで
public class MobilePhone { 

private ArrayList<Contacts> contactList = new ArrayList<Contacts>(); 

//searchContactByName() method takes the contact name you want to search and 
//return the contact object with that contact name if it's in the list, and return null if the contact is not on the list 

public Contacts searchContactByName(String contactName){ 
     Contacts returnContact = new Contacts(); 
     boolean contactExist = false; 
     for(int i = 0; i < contactList.size(); i++){ 
      if(contactList.get(i).getContactName() == contactName){ 
       System.out.println("Contact " + contactName + " found"); 
       contactExist = true; 
       returnContact = contactList.get(i); 
       break; 
      } 
     } 

     if(contactExist == true){ 
      return returnContact; 
     }else{ 
      System.out.println("not found"); 
      return null; 
     } 
    } 

//addContact2() method use the searchContactByName() method to make sure 
//that the contact you add does not already exists in the list 

public void addContact2(String contactName, String phoneNumber){ 
     Contacts newContact = new Contacts(contactName,phoneNumber); 
     if(searchContactByName(contactName) != null){ 
      System.out.println("This contact is already in the contact list."); 
     }else{ 
      contactList.add(newContact); 
      System.out.println(contactName + " has been added to the list"); 
     } 
    } 

しかし、私は機能を実装しよう:

public class TestMain { 
    public static Scanner myScanner = new Scanner(System.in); 
    public static void main(String[] args) { 

    MobilePhone jolenePhone = new MobilePhone(); 
    //First add a contact object with contactName "Rish" in the list: 
    jolenePhone.addContact2("Rish","1234"); 

    //Then use searchContactByName() function to search the contact object with name "Rish" in the list: 
    jolenePhone.searchContactByName("Rish"); 
    } 
    } 

これは、コンソールに出力して正常に動作します: enter image description here

しかし、私はスキャナを使用する場合関数:

MobilePhone jolenePhone = new MobilePhone(); 
     //First add a contact object with contactName "Rish" in the list: 

     jolenePhone.addContact2("Rish","1234"); 
     System.out.println("Please enter search name: "); 
     String searchName = myScanner.nextLine(); 
     jolenePhone.searchContactByName(searchName); 

次に、「Rish」という連絡先名に対してsearchFunctionが返されませんでした。 enter image description here

私はcontactNameパラメータ "Rish"を直接渡すかどうかは分かりません。コンソールから入力するにはscanner.nextLine()関数を使用します。私はここで恋しいですか?

+2

を無視する==使用equals()またはequalsIgnoreCase()を使用いけない:***(contactList.get(I).getContactName()==場合CONTACTNAME *** –

+0

'==は'として働きますあなたの文字列が文字列プールから来ている間は、この質問をチェックしてください:https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – JohnnyAW

答えて

0

変更== if(contactList.get(i).getContactName() == contactName)~equals()です。 ==は参照の平等のためのもので、equalsはオブジェクトの平等のためのものです。

0

が、これは間違っている場合sensitiev