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]
}
}
...
...
}
}
return counterarray;counterarray[9].