2016-12-06 33 views
-1

我有3只狗的Arraylist,我可以在列表中註冊更多的狗,通過調用toString函數列出它們,將狗的年齡增加1,並從列表中移除狗。只要你只使用最後兩個,remove()increaseAge()它們都可以工作,但只要你使用任何其他的命令,它們就不會找到任何狗,併發送回-1,因爲它不應該按名稱找到任何狗。所以它是remove()increaseAge()這很奇怪。什麼使這些功能在第一次輸入後不起作用?

主要代碼:

public class DogTest { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 
     ArrayList<Dog> Dogs = new ArrayList<Dog>(); 
     DogFunctions DF = new DogFunctions(); 
     Dogs.add(new Dog("Peggy", "Labrador", 9, 30)); 
     Dogs.add(new Dog("Max", "Tax", 4, 13)); 
     Dogs.add(new Dog("Sanna", "Schäfer", 7, 25)); 
     Boolean loop = true; 

     while(loop){ 
      String input = keyboard.nextLine(); 
      switch (input){ 
       case "Register": 
        Dogs.add(DF.register()); 
        break; 
       case "IncreaseAge":   
        Dogs.get(DF.returnDogIndex(Dogs, "Input Message", "Output Message")).increaseAge(); 
        break; 
       case "List": 
        DF.list(Dogs); 
        break; 
       case "Remove": 
        Dogs.remove(DF.returnDogIndex(Dogs, "Input Message", "Output Message")); 
        break; 
       case "Quit": 
        loop = false; 
        break; 
      } 

     } 
     keyboard.close(); 
    } 

} 

代碼的功能:

import java.util.ArrayList; 
import java.util.Scanner; 

public class DogFunctions { 

    private Scanner keyboard = new Scanner(System.in); 

    public Dog register(){ 
     System.out.println("Namn:"); 
     String name = keyboard.nextLine(); 
     System.out.println("Ras:"); 
     String breed = keyboard.nextLine(); 
     System.out.println("Ålder:"); 
     int age = keyboard.nextInt(); 
     System.out.println("Vikt:"); 
     double weight = keyboard.nextDouble(); 
     Dog d = new Dog(name, breed, age, weight); 
     System.out.println("Hund tillagd i registret"); 
     return d; 
    } 

    public void list(ArrayList<Dog> Dogs){ 
     System.out.println("Ange svanslängd:"); 
     double input = keyboard.nextDouble(); 
     if(input == 0){ 
      for (int i = 0; i < Dogs.size(); i++){ 
       System.out.println(Dogs.get(i).toString()); 
      } 
     } 
     else{ 
      for (int i = 0; i < Dogs.size(); i++){ 
       if(Dogs.get(i).getTailLenght() >= input){ 
        System.out.println(Dogs.get(i).toString()); 
       } 
      } 
     } 

    } 

    public int returnDogIndex(ArrayList<Dog> Dogs, String inputMessage, String outputMessage){ 
     System.out.println(inputMessage); 
     String input = keyboard.nextLine(); 
     for(int i = 0; i < Dogs.size(); i++){ 
      if(input.equals(Dogs.get(i).getName())){ 
       System.out.println(outputMessage); 
       return i; 
      } 
     } 
     return -1; 
    } 

} 

這是這最後的功能 「returnDogIndex」,我認爲是有點問題。

代碼爲狗類:

public class Dog { 

    private String breed, name; 
    private int age; 
    private double weight, tailLength; 

    public Dog(String name, String breed, int age, double weight) { 
     this.name = name; 

     this.breed = breed; 
     this.age = age; 
     this.weight = weight; 
     calcTailLength(); 
    } 

    public void calcTailLength(){ 
     if (breed.toLowerCase().equals("tax")) { 
      tailLength = 3.7; 
     } 
     else { 
      tailLength = (age*weight)/10; 
     } 
    } 

    public String toString() { 
     return name + " är en " + age + " år gammal " + breed + " som väger " + weight + " kg och har en svanslängd på " + tailLength; 
    } 

    public String getName() { 
     return name; 
    } 

    public void increaseAge(){ 
     age++; 
     calcTailLength(); 
    } 

    public double getTailLenght(){ 
     return tailLength; 
    } 

} 
+0

對不起壞的標題和這樣 – Olof

+0

這情況下,你要什麼都不用做? 「列表」和「刪除」? –

+0

我確實需要它們,因爲它們是作業的一部分。但是在任何命令輸入之後,「刪除」和「增加年齡」不起作用,這是前面提到的。 – Olof

回答

1

您返回-1作爲指標,當你不找狗。這是第一個錯誤。指數在Java中的數組或ArrayList中從0開始,因此返回-1會給你ArrayIndexOutOfBoundsException異常異常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 
at java.util.ArrayList.elementData(ArrayList.java:382) 
at java.util.ArrayList.remove(ArrayList.java:459) 
at com.daimler.iqm.service.DogExec.main(DogExec.java:30) 

在這旁邊,你一直在做的最大的錯誤是一步獲取特定塔伊的輸入

keyboard.nextDouble(), keyboard.nextInt() 

我會建議你去通過這些2個StackOverflow的線程來認識和解決您的問題

Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Java String Scanner input does not wait for info

這就是說,你需要糾正你2種方法註冊和列表,並nextInt或nextDouble的每次通話後添加keyboard.nextLine()

public Dog register(){ 
    System.out.println("Namn:"); 
    String name = keyboard.nextLine(); 
    System.out.println("Ras:"); 
    String breed = keyboard.nextLine(); 
    System.out.println("Ålder:"); 
    int age = keyboard.nextInt();keyboard.nextLine(); 
    System.out.println("Vikt:"); 
    double weight = keyboard.nextDouble();keyboard.nextLine(); 
    Dog d = new Dog(name, breed, age, weight); 
    System.out.println("Hund tillagd i registret"); 
    return d; 
} 

public void list(ArrayList<Dog> Dogs){ 
    System.out.println("Ange svanslängd:"); 
    double input = keyboard.nextDouble();keyboard.nextLine(); 
    if(input == 0){ 
     for (int i = 0; i < Dogs.size(); i++){ 
      System.out.println(Dogs.get(i).toString()); 
     } 
    } 
    else{ 
     for (int i = 0; i < Dogs.size(); i++){ 
      if(Dogs.get(i).getTailLenght() >= input){ 
       System.out.println(Dogs.get(i).toString()); 
      } 
     } 
    } 

} 
+0

停止發佈重複問題的答案並將其標記出來。 – Tom

+0

@Tom,以及這是一個重複的問題 – Acewin

+0

閱讀鏈接問題的答案,看看它們如何解決解釋的問題,然後檢查解決問題的方式,然後您就會知道它。如果沒有,那麼請查看關於重複問題的幫助頁面:http://stackoverflow.com/help/duplicates – Tom

相關問題