0

Alright so I've created an array of type int with size 10.

I've initialized it to random values between 1-100.

Now my task is to write a method named output which will take an int array as a parameter and displays all elements of the array in a column like this:

Output

arr[0] : 17

arr[1] : 42

etc etc, but when I do it in eclipse it says

i cannot be resolved to a variable, 

so I was thinking of re-initializing it in my method, but wouldn't that give me a whole set of different numbers?

private int [] nums;

public UsingArrays(){
    nums  = new int [10];
    for (int i = 0; i <nums.length; i++){
        nums[i] = (int)(Math.random()*100)+1;
    }
}

public String Output(){
    String string;
    string = "Arr[" + i + "]:" + nums[i];
    return string;

}




}
2
  • Do you have i variable declared as object property? Commented Jan 19, 2016 at 22:58
  • 2
    what is i in Output() ? you are missing a loop around it as well Commented Jan 19, 2016 at 22:58

3 Answers 3

1

i cannot be resolved to a variable

You forgot to surround the whole thing with a for loop that will be declaring & using that i variable :

public void Output(int[] array){
    String string = "";

    for(int i = 0; i < array.length; i++) {
        string += "Arr[" + i + "]:" + array[i] + "\n"; // not recommended
    }

    System.out.println(string);
}

And in such cases, it would be better if you use a StringBuilder, so as to avoid creating new String instances at each iteration:

public void Output(int[] array){
    StringBuilder sb = new StringBuilder();

    for(int i = 0; i < array.length; i++) {
        sb.append("Arr[" + i + "]:" + array[i] + "\n"); // Accumulate results
    }

    System.out.println(sb.toString()); // print final result
}
Sign up to request clarification or add additional context in comments.

4 Comments

Clearly this is someone trying to learn the basics of the language, and is obviously part of an assignment. Inundating them with meaningless details about StringBuilder serves no good. Also, just giving them the answer without explaining why their original code is wrong does the poster no good. At least explain why their original code doesn't work.
You've also missed the other requirement, which is the passing of an int[] parameter to the method.
@Makoto my bad, thanks for pointing that out. (Notice that he declared nums as a member variable, so technically he won't be needing it...)
I'm well aware of how the array is declared, but again, that's not part of the actual requirement.
0

I do not know why you are trying to initialize int[] in a constructor and keep this array as a global variable when you said that 'method named output which will take an int array as a parameter'. Here is a proper solution according to your requirements:

public static void main(String[] args) {
    final int sizeOfArray = 10;
    final int[] nums = new int[sizeOfArray];
    //JAVA 7
    for(int i = 0; i < sizeOfArray; i++){
        nums[i] = (int)(Math.random() * 100) + 1;
    }
    //JAVA 8
    IntStream.range(0,sizeOfArray)
        .forEach(i -> nums[i] = (int)(Math.random() * 100) + 1);

    output(nums);
}

private static void output(final int[] arrayToDisplay){
    //JAVA 7
    for(int i = 0; i < arrayToDisplay.length; i++){
        System.out.printf("arr[%d] : %d \n",i,arrayToDisplay[i]);
    }
    //JAVA 8
    IntStream.range(0,arrayToDisplay.length)
        .forEach(i -> System.out.printf("arr[%d] : %d \n",i,arrayToDisplay[i]));
}

You should always make sure that all variables initialized and have an appropriate type assigned to them (at least in Java). If I were you, I would stick to Java 7 version of the code above

Comments

0

This problem won't be complied because there is not definition of what 'i' means in output method.

Another solution can be :

public String Output(){
    StringBuffer sb = new StringBuffer();
    int i=0;
    while(i<nums.length){
        sb.append("Arr[" + i + "]:" + nums[i]);
        System.out.println(sb.toString());
        sb.clear();
    }
}

Comments

Your Answer

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