I am trying to complete an exercise where I have some tasks including this one: Print only the multiples of 3 in the array I have to use an applet and I don't know how to do it. I tried to set a condition on the graphic part but it returns a not nice 0
public void init() {
dataList = new int[17];
int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};
for (int i = 0; i < dataList.length; i++) {
//Compute the sum of the elements in the array.
sum += dataList[i];
//Compute the product of the elements in the array.
product *= dataList[i];
//Compute the frequency of the number 5 in the array
if (dataList[i] == 5) {
fiveCounter++;
}
}
}
public void paint(Graphics g) {
g.drawString(("Sum of elements is: " + sum), 25, 25);
g.drawString(("Product of elements is: " + product), 25, 50);
g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
for (int i = 0; i < dataList.length; i++) {
if ((dataList[i] % 3) == 0) {
g.drawString((String.valueOf(dataList[i])), 25, 100);
}
}
}
in another attempt where I tried to create a new array based on the calculation of the number of values multiple of 3, the program does not start and I get ArrayIndexOutOfBoundException
public void init() {
dataList = new int[17];
multiple3 = new int[mult3Counter];
int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};
for (int i = 0; i < dataList.length; i++) {
//Compute the sum of the elements in the array.
sum += dataList[i];
//Compute the product of the elements in the array.
product *= dataList[i];
//Compute the frequency of the number 5 in the array
if (dataList[i] == 5) {
fiveCounter++;
}
if ((dataList[i] % 3) == 0) {
multiple3[i] = dataList[i];
mult3Counter++;
}
}
public void paint(Graphics g) {
g.drawString(("Sum of elements is: " + sum), 25, 25);
g.drawString(("Product of elements is: " + product), 25, 50);
g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
for (int i = 0; i < multiple3.length; i++) {
g.drawString((String.valueOf(multiple3[i])), 25, 100);
}
}
How can I solve this?