2

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?

Adam Stelmaszczyk
  • 18,835
  • 4
  • 63
  • 104
JohnSnow
  • 163
  • 3
  • 14
  • Hint: the error message `ArrayIndexOutOfBoundException` means that you tried to iterate over an index beyond the length of your array. The issue is here: `for (int i = 0; i < dataList.length; i++)` and `for (int i = 0; i < multiple3.length; i++)` Since the first index of an array is zero, you actually want to iterate through `array.length - 1`. – femmestem Dec 13 '15 at 21:06
  • 1
    No. This isn't the problem. It will iterate as it checks __lesser than__ - not __lesser or equal__ – ThisaruG Dec 13 '15 at 21:07
  • the compiler is spitting fire here: multiple3[i] = dataList[i]; I tried as suggested by Thisaru Guruge with no luck. – JohnSnow Dec 13 '15 at 21:29
  • 1
    As I mentioned in my answer, you __MUST__ use two different counters. `multiple3[i] = dataList[i];` will not work. `multiple3[j] = dataList[i];` will do – ThisaruG Dec 13 '15 at 21:34
  • It compiles but no input is displayed for the multiple of 3 and when executed it throws Exception error. – JohnSnow Dec 13 '15 at 21:53
  • The task being `Print only the multiples of 3 in the array`, why bother creating a data structure holding only these instead of printing them immediately? That said, you _can_ pass an array as a parameter. If you intend to collect something you don't know the exact number of, declare a `Collection` and instantiate something about the right size: `Collection extends Number> multiples = new ArrayList(dataList.length/3+2);` (providing `add()`, `Collection` doesn't need a counter, you can use it in a _foreach-loop_ or use that newfangled `Iterable.forEach()` … – greybeard Dec 14 '15 at 08:02

2 Answers2

1

You can't use the same counter for both arrays. Use two different counters. You can't use mult3Counter as the size of the array multiple3 as it is not initialized ! Hence, mult3Counter will be 0 by default. So when you are going to access multiple3[] with any index, it will give ArayIndexOutOfBoundsException

If you need to know the count of the occurrences of the multiples of 3, you have to run loop twice;

int mult3Counter = 0;
for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    mult3Counter++;
  }
}

int j = 0;
int [] multiple3 = new int[mult3Counter];

for (i = 0; i < dataList.length; i++)
{
  if ((dataList[i] % 3) == 0)
  {
    multiple3[j++] = dataList[i];
  }
}

Or the best way is to use List (ArrayList) to add multiples of 3.

ArrayList<int> multiple3 = new ArrayList<>();

for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    multiple3.add(dataList[i]);
  }
}

If you need an array, you can convert it into an array later. Refer This Question

Community
  • 1
  • 1
ThisaruG
  • 2,382
  • 5
  • 34
  • 50
0

I solved question on my own, by changing the code in the Graphics part only. the only ugly thing is that I had to repeat the dataList as I couldn't transfer the value from the init method:

public void paint(Graphics g) {
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};
  int yposition = 100;

  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);
  g.drawString(("The following numbers are multiple of 3"), 25, yposition);

  for (int i = 0; i < dataList.length; i++) {
    if ((dataList[i] % 3) == 0) {
      yposition += 15;
      g.drawString((String.valueOf(dataList[i])), 25, yposition);
    }
  }
}
ThisaruG
  • 2,382
  • 5
  • 34
  • 50
JohnSnow
  • 163
  • 3
  • 14
  • 1
    I answered the question you asked - __Print only the multiples of 3 in the array__ We can't know what's on your mind right. If you asked how to adjust your position when multiple of 3 occurs, we will answer that. – ThisaruG Dec 14 '15 at 07:47