0

First of all, it would be best to look at the following question first, for this question to make more sense. How can I reduce this long list of if statements?

The values of the array int[] counterarray = new int[10]; are the following:

counterarray[0] is 3                                                                
counterarray[1] is 3                                                           
counterarray[2] is 0                                                             
counterarray[3] is 0                                                                  
counterarray[4] is 1                                                              
counterarray[5] is 5                                                               
counterarray[6] is 0                                                          
counterarray[7] is 0                                                             
counterarray[8] is 1                                                              
counterarray[9] is 2      

This piece of code determines the above values fro each of the elements of the array:

counterarray = new int[10];

for (int x = 14; x >= 0; x--) {
    if (anArray[x] >= 0 && anArray[x] < 101) {
        int idx = Math.min(anArray[x] / 10, 9);
        ++counterarray[idx];
    }
}

It should not be modified.

I am trying to return the value of certain elements in the array from the method.

So I want to be able to return only a given element of the array in a given line of the code, when I want and how I want it. How do I do that?

When I compile the code, in the method public static int[] countarraymethod there is an error on the line indicated above.

Error: Syntax error, insert ". class" to complete Expression

When I put a number inside the square brackets, this error comes instead:

Error: Type mismatch: cannot convert from int to int[]

By the way, the reason I put the line // return only counterarray[9] as comments is because I don't know how to make a command that will return only the given element of the array.

import java.io.*;
import java.util.*;

public class GradesHistogram {         
    public static int[] countarraymethod (int[] counterarray, int[] anArray, int counterrepeat) {
        counterarray = new int[10];
        for (int x = 14; x >= 0; x--) {
            if (anArray[x] >= 0 && anArray[x] < 101) {
                int idx = Math.min(anArray[x] / 10, 9);
                ++counterarray[idx];
            }
        }
        for (counterrepeat = 0; counterrepeat < 10; counterrepeat++) {
            System.out.println("counterarray[" +counterrepeat+ "] is " +counterarray[counterrepeat]);
        }
        return counterarray[];  //error is here
    }
    public static void main(String[] args)throws java.io.IOException {
        ...
        ...
        int histogrammin = 0;
        int histogrammax = 0;
        x = readnumber;
        for (int histogramrows = 0; histogramrows < 10; histogramrows++) {

            histogrammin = histogramrows * 10;

            if (histogrammin == 90) {
                // return only counterarray[9]    
            } else {
                histogrammax = histogrammin + 9;

                // return counterarray[0]
                // return counterarray[1]
                // return counterarray[2]
                ...
                ...
                // return counterarray[8]
            }

        }
        ...
        ...
    }
}
4
  • 3
    return counterarray; Commented Dec 2, 2015 at 9:01
  • 1
    If you want people to read your code, I suggest you format it or let your IDE do it for you. Commented Dec 2, 2015 at 9:02
  • If you want to return a single element, you should specify index of that element. Say counterarray[9]. Commented Dec 2, 2015 at 9:08
  • Can you show your complete code because from this code it is not clear what you want to do ? And why you are passing the counterarray as argument what is its purpose because you are initializing it in method. Commented Dec 2, 2015 at 9:09

2 Answers 2

3

The return type of your method is int[] so you must return an array of integers. If you want to return only one element of your array you should change the return type of the method to int and return the value like this:

return array[index];

The syntax you currently have is not valid, hence the compilation error. When returning an array you don't need to put the square brackets after the variable name.

Sign up to request clarification or add additional context in comments.

Comments

1

One specific item in a int[] array is just an int. Change the return type to int and then use:

return counterarray[index];

Also, a bit off-topic, it´s usually a good practice to name variables like this:

return counterArray[index]; //First letter of every word in Uppercase (Except for the first one)

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.