2017-09-05 4 views
-2

Unten ist der Code. Ich kann nicht herausfinden, warum in der Datenbank mein else sonst, wenn der Staat gleichzeitig mit meiner if-Anweisung läuft, wenn ich den zweiten Vogel in die ArrayList eingegeben habe. Bitte, jede Hilfe wäre willkommen!Wenn Anweisungen gleichzeitig ausgeführt werden, wenn zwei Objekte in ArrayList

public class Main { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     Database d1 = new Database(); 

     while (true) { 
      System.out.println("What do you want to do?"); 
      String answer = input.nextLine(); 

      if(answer.equalsIgnoreCase("Add")){ 
       System.out.println("Name: "); 
       String name = input.nextLine(); 
       System.out.println("Latin name: "); 
       String lName = input.nextLine(); 

       d1.addBird(name, lName); 
      } else if (answer.equalsIgnoreCase("O")) { 
       System.out.println("What was observed?"); 
       String observed = input.nextLine(); 
       d1.Observation(observed); 
      } else if (answer.equalsIgnoreCase("stats")) { 
       d1.showBirds(); //Displays all with observations. 
      } else if (answer.equalsIgnoreCase("show")) { 
       System.out.println("What?"); 
       String search = input.nextLine(); 
       d1.searchBird(search); 
      } else if (answer.equalsIgnoreCase("quit")){ 
       break; 
      } 
     } 
    } 
} 

public class Bird { 

    private final String name; 
    private final String latinName; 
    private int count; 

    public Bird (String name, String latinName) { 
     this.name = name; 
     this.latinName = latinName; 
     this.count = count; 
    } 

    public String getName() { 
     return this.name; 
    } 

    public String getLatin() { 
     return this.latinName; 
    } 

    public String add() { 
     return this.name + " " + "(" +this.latinName + ")"+ " " + this.count + " observation(s)"; 
    } 

    public void increaseCount() { 
     this.count++; 
    } 
} 

import java.util.ArrayList; 

public class Database { 

    private final ArrayList<Bird> birdList; 

    public Database() { 
     this.birdList = new ArrayList<Bird>(); 
    } 

    public void addBird (String name, String lname) { 
     this.birdList.add(new Bird(name, lname)); 
    } 

    public void Observation (String observed) { 
     for (Bird x : getBirds()) { // this has to be a method 
      if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) { 
       System.out.println("Done"); 
       System.out.println(""); 
       x.increaseCount();  
      } else if (x.getName() != observed || x.getLatin() != observed) { 
       System.out.println("Not a bird"); 
      } 
     } 
    } 

    public void showBirds() { 
     for (Bird x : this.birdList) { 
      System.out.println(x.add()); 
     } 
    } 

    public ArrayList<Bird> getBirds() { 
     return this.birdList; 
    } 

    public void searchBird(String search) { 
     for (Bird x : getBirds()) { 
      if (x.getName().contains(search)) { 
       System.out.println(x.add()); 
      } 
     } 
    } 
} 
+0

wo ist die Datenbank-Klasse Code? posten Sie es hier und Ihre Eingabe Ausgabe erhalten –

+2

Was meinst du "läuft gleichzeitig"? Das ist eine wirklich unklare Frage. – John3136

Antwort

0

denke ich, das Problem bei diesem Verfahren liegt:

public void Observation (String observed) { 

    for (Bird x : getBirds()) { // this has to be a method 
     if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) { 
      System.out.println("Done"); 
      System.out.println(""); 
      x.increaseCount();  
     } 
     /* No need to print "Not a bird" for every mismatch. Use a flag instead */ 
     else if (x.getName() != observed || x.getLatin() != observed) { 
      System.out.println("Not a bird"); 
     } 
    } 
} 

etwas tun:

public void Observation (String observed) { 
    boolean found = false; 
    for (Bird x : getBirds()) { // this has to be a method 
     if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) { 
      System.out.println("Done"); 
      System.out.println(""); 
      x.increaseCount(); 
      found = true;  
     } 
    } 
    if (!found) { 
      System.out.println("Not a bird"); 
    } 
} 
+0

Warum der Downvote? –

+0

Dies behebt das Problem! Ich muss noch so viel lernen. Ich hätte nie gedacht, dass ein boolescher Wert das Problem lösen würde. Habe den ganzen Tag damit verbracht, mir die Haare auszuziehen. Vielen Dank! – Dyeh310

+0

Akzeptieren Sie die Antwort, wenn das Problem gelöst wird. –

Verwandte Themen