2017-08-23 12 views
0

Ich mache eine Codierung Übung, um Kontaktliste in einem Handy zu simulieren, können Sie bestehenden Kontakt suchen und neuen Kontakt von Systemeingang hinzufügen, jedoch stieß ich auf einige Probleme mit Scannern und nicht warum weiß: (ich würde wirklich schätzen Ihre Hilfe, so dank im Voraus)Java: Scanner und ArrayList

Hier ist mein Code: Zunächst einmal eine Klasse für Kontakte:

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; 
} 
} 

Dann habe ich eine Mobilephone-Klasse zu halten der Kontakt ArrayList, mit addContact() und searchContactByName() Methode:

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"); 
     } 
    } 

jedoch im Hauptverfahren, wenn ich versuche, um die Funktion zu implementieren:

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"); 
    } 
    } 

Dies würde an der Konsole normalerweise mit dem Ausgang arbeiten: enter image description here

Allerdings, wenn ich die Scanner verwenden Funktion:

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); 

Dann würde die searchFunction zurückgegeben nicht für den Kontaktnamen "Rish" I Eingabe gefunden über die Konsole: enter image description here

Ich verstehe nicht, warum es überhaupt einen Unterschied, ob ich den contact Parameter „Rish“ direkt übergeben, oder ich benutze den scanner.nextLine() -Funktion von der Konsole eingegeben wird, was habe ich hier vermisst?

+2

das ist falsch: *** if (contactList.get (i) .getContactName() == contact *** –

+0

'==' arbeitet als Solange Ihre Zeichenfolge aus dem String-Pool stammt, überprüfen Sie diese Frage: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – JohnnyAW

Antwort

0

Änderung == in if(contactList.get(i).getContactName() == contactName) zu equals(). == ist für die Referenzgleichheit, equals ist für die Objektgleichheit.

0

nicht == Verwendung equals() oder equalsIgnoreCase() verwenden zu Fall ignorieren sensitiev