-1

I have an Arraylist of 3 dogs, and I can register more dogs to the list, list them by calling a toString function, increase the age of a dog by one, and remove a dog from the list. As long as you only use those two last ones, remove() and increaseAge() they both work, but as soon as you use any of the other commands, they can't find any dogs and send back -1 as it should when it doesn't find any dogs by the name given. So it's remove() and increaseAge() that's being weird.

Main code:

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

}

Code for the functions:

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

}

It's this last function "returnDogIndex" that I think is something wrong with.

Code for the Dog class:

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

}
Olof
  • 1
  • 2
  • Sorry for bad title and such – Olof Dec 06 '16 at 23:01
  • which cases do not do what you want? "List" and "Remove"? – RAZ_Muh_Taz Dec 06 '16 at 23:02
  • I do want them all, since they are part of the assignment. But "Remove" and "Increase Age" aren't working after any command has been enterd that isn't one of previously mentioned. – Olof Dec 06 '16 at 23:06
  • so you never get the outputMessage you send to the index method to be print to the screen because it never finds it? try printing out what input is in your index method – RAZ_Muh_Taz Dec 06 '16 at 23:09
  • just show us the output you get from running your program – RAZ_Muh_Taz Dec 06 '16 at 23:12
  • Error message is IndexOutOfBounds since it returns -1. When I enter "Remove", it outputs the inputMessage but then immediately crashes as if I would have enterd something wrong. – Olof Dec 06 '16 at 23:17
  • You could use a debugger and step through your program to see the values when it crashes. If you don't want to do that, a https://stackoverflow.com/help/how-to-ask would be likely to get more answers. – Robert Dec 06 '16 at 23:39
  • How would I go about using a debugger? – Olof Dec 06 '16 at 23:46
  • List and Register work just fine for me. You are misinterpretating ArrayOutOfBOund exception as -1 response – Acewin Dec 06 '16 at 23:54
  • @Olof are you using eclipse or any other IDE – Acewin Dec 06 '16 at 23:54
  • Im using eclipse. List and Register aren't the ones that Im having problem with, it's Remove and Increase age that's being weird. – Olof Dec 06 '16 at 23:59
  • Go through this tutorial to understand debugging in Eclipse. http://www.vogella.com/tutorials/EclipseDebugging/article.html – Acewin Dec 07 '16 at 00:14

1 Answers1

1

You are returning -1 as index when you do not find dog. This is the first error. Index in Java for an Array or ArrayList starts from 0, therefore returning -1 will give you ArrayIndexOutOfBoundsException exception

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)

Beside this the biggest mistake you have been doing is the step to get an input of a specific tye

keyboard.nextDouble(),  keyboard.nextInt() 

I will suggest you to go through these 2 StackOverflow threads to understand and resolve your issue

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

Java String Scanner input does not wait for info

That said you need to correct your 2 methods register and list and add keyboard.nextLine() after each call of nextInt or nextDouble

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

}
Community
  • 1
  • 1
Acewin
  • 1,523
  • 3
  • 15
  • 32
  • Stop posting answers to duplicate questions and flag them instead. – Tom Dec 07 '16 at 18:19
  • @Tom, and how is this a duplicate question – Acewin Dec 07 '16 at 18:29
  • Read the answers of the linked questions and see how they fix the explained issue and then check how you fix the explained issue, then you'll know it. If not, then check the help page about duplicate questions: http://stackoverflow.com/help/duplicates – Tom Dec 07 '16 at 18:38
  • True but then you cannot mark it duplicate for multiple questions. Because I understand this will be duplicate to 1-> How to avoid ArrayIndexOutOfBoundsException, 2 -> How to debug in eclipse, 3 -> the question you and me both have linked. I will leave this to your discretion. No offense intended in this comment about the process. Clearly the OP is not getting the whole idea of coding and debugging. Which everyone has pointed out in the comment – Acewin Dec 07 '16 at 18:49
  • 1) It shouldn't be a dupe of this one, because the AIOOBE is not the root cause and just a "symptom" of the real issue. 2) that would be a rather broad dupe and one should use the most specific duplicate question, if known/available 3) bingo ;P. You can still write a comment to add further infos next to the dupe link, if you want. *"No offense intended"* none taken, no worries. – Tom Dec 07 '16 at 19:39
  • true. After finding the issue and it being a repeated issue I shud have marked it as duplicate and let OP figure out why – Acewin Dec 07 '16 at 21:23
  • *"let OP figure out why"* you can still write comments and point OP into the right direction, why the linked question answers the main question. – Tom Dec 07 '16 at 22:33