0

I just studied Arrays but I can't use them in a for loop and I can't understand what's wrong with the code and how to actually get this to work.

import java.util.*;

public class DemoCane {

    Scanner sc = new Scanner (System.in);
    public static int[] cani;

    public static void main (String args []){
        setQuantitaCani();
        for (int i=0; i<=cani[nrcani].lenght; i++){
            Cane cani[i] = new Cane();
            cani[i] = Cane.setInfo();
            cani[i].getInfo();
        }

    }

    public static void setQuantitaCani(){
        System.out.println("Quanti cani vuoi aggiungere?");
        int nrcani = sc.nextInt();
        cani[] = new int[nrcani];
    }
}

I wrote the code this way because I want to get an input from the user about how many dogs he want to enter (cani means dogs). Then, for every dog it creates a new dog object from the dog class (in another file, which has compiled fine) and the user has to set his informations. Shall I use a while loop instead?

EDIT: Every error is in this line:

cani[] = new int[nrcani];
7
  • 2
    Watch the spelling for "length". You spelled it as "lenght". That won't compile. And don't use <= in the condition. An array of 5 elements will have indices from 0 to 4. Running to index 5 causes an ArrayIndexOutOfBoundsException. Commented Jan 18, 2018 at 10:01
  • 1
    Cane cani[i] = new Cane(); should be cani[i] = new Cane();. also your array is of type int and not Cane Commented Jan 18, 2018 at 10:02
  • 1
    What is Cane? setInfo? getInfo? Commented Jan 18, 2018 at 10:03
  • @SurfMan Corrected, thanks. Commented Jan 18, 2018 at 10:08
  • @XtremeBaumer There I created a new object for the class Cane (declared in another file). Isn't the correct form Class name of obj = new _Class_() ? Commented Jan 18, 2018 at 10:08

1 Answer 1

2

Updated your code a bit:

public class DemoCane {

    static Scanner sc = new Scanner(System.in);
    private static Cane[] cani;

    public static void main(String args[]) {
        setQuantitaCani();
        for (int i = 0; i < cani.length; i++) {
            cani[i] = new Cane();
            cani[i] = cani[i].setInfo();
            cani[i].getInfo();
        }

    }

    public static void setQuantitaCani(){
        System.out.println("Quanti cani vuoi aggiungere?");
        int nrcani = sc.nextInt();
        cani = new Cane[nrcani];
    }
}

onto the explanation:

  1. If you want to add Cane-objects to your array, then the array must have that type (private static Cane[] cani; and cani[] = new Cane[nrcani];)
  2. Accessing the array length is done by array_variable_name.length. What you did is only needed for 2-dimensional arrays
  3. If you work in the main-method your variables have to be static as well
  4. Adding new objects to the array is done with array_variable_name[index] = new Classname();. You dont have to do Cane cani[i] = new Cane();, because the array is already of type Cane
  5. Initiating the array works like this: Cane[] cani = new Cane[10]. You only need the [] if you want to access an index, but not while initiating (except when declaring the amount of elements)
  6. cani[i] = Cane.setInfo(); I assume you want to access the Cane at the index and set the info for that object. Since we don't know if your Cane-class actually has the method public static void setInfo(), i changed it to cani[i] = cani[i].setInfo(); to set the info
  7. As already stated in the comments, arrays are 0-based. This means the first element you can access is at cani[0]. Therefore you have to be careful not to exceed the bounds (0 til (nrcani-1))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.