1

I have an integer array like this

int[] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

Now I want to change every multiple of 3 (but not multiple of 5) to 'AB', every multiple of 5 ((but not multiple of 3) to 'CD' & every every multiple of both 3 & 5 to 'ABCD

I want a dynamic solution which can be used for any number of elements of array - that the solution should stand irrespective of size.

I was trying like this

for (int i = 0; i <= n; i++) {
        if ((i % 3 == 0) || (i % 5 != 0)) {
            arr[i] = 'AB';
        }
        else if ((i % 3 != 0) || (i % 5 == 0)) {
            arr[i] = 'CD';
        }
        else if ((i % 3 == 0) || (i % 5 == 0)) {
            arr[i] = 'ABCD';
        }
    }

But it is giving error in Eclipse as i am unable to assign string value in integer array. I am beginner in Java, that is why I am requesting someone to help me in this regard.

The final array will be [1, 2, AB, 4, CD, AB, 7, 8, AB, CD, 11, AB, 13, 14, ABCD, 16, 17, AB, 19, CD];

tommy123
  • 587
  • 1
  • 10
  • 28
  • 2
    An `int[]` can hold `int` values. A `char` is an integral type. The issue is `'AB'` is **two** characters, and thus is not a valid character. Have you considered `'C'`? – Elliott Frisch Dec 30 '19 at 03:48
  • @Elliott Frisch I want to change every multiple of 3 (but not multiple of 5) to 'AB', every multiple of 5 ((but not multiple of 3) to 'CD' & every every multiple of both 3 & 5 to 'ABCD – tommy123 Dec 30 '19 at 03:51

1 Answers1

0

This looks to be a modified version of Fizz buzz. There's a couple mistakes that I can spot off the bat.

if ((i % 3 == 0) || (i % 5 != 0)) {

In the above snippet, you're checking if i (the iterator of the for-loop) is a multiple of 3 and 5. However, you want to check if the array element at position i is a multiple of 3 or 5. To do that, you can change the above snippet to the following:

if ((arr[i] % 3 == 0) || (arr[i] % 5 != 0)) {

Next, your conditions seem to be incorrect. You want to check if the element at position i is a multiple of 3 AND the element at position i is not a multiple of 5. To do that, you should use && instead of ||, like so:

if ((arr[i] % 3 == 0) && (arr[i] % 5 != 0)) {

Lastly, you're trying to store more than one character into a single char value, which isn't possible in Java. If you're required to use more than one character, then you should use a String instead. However, you won't be able to store a String in an int[], so you'll need to create a String[]:

String[] newArray = new String[arr.length];

for (int i = 0; i <= n; i++) {
    if ((arr[i] % 3 == 0) && (arr[i] % 5 != 0)) {
        newArray[i] = "AB";
    } else if ((arr[i] % 3 != 0) && (arr[i] % 5 == 0)) {
        newArray[i] = "CD";
    } else if ((arr[i] % 3 == 0) && (arr[i] % 5 == 0)) {
        newArray[i] = "ABCD";
    } else {
        newArray[i] = Integer.toString(arr[i]);
    }
}

We also needed to add a case for when all conditions are false, as we want to retain the initial values in arr that don't satisfy any condition.


Taking n to be arr.length - 1, we can print the contents of newArray with:

System.out.println(Arrays.toString(newArray));

Which produces the expected output:

[1, 2, AB, 4, CD, AB, 7, 8, AB, CD, 11, AB, 13, 14, ABCD, 16, 17, AB, 19, CD]
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • Hi I have changed the question a bit. Can you check now? – tommy123 Dec 30 '19 at 03:54
  • @tommy123 I've edited my answer to accommodate the modifications made to your question. – Jacob G. Dec 30 '19 at 04:02
  • System.out.println(Arrays.toString(newArray)); when printing this line I am getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at SixthProblem.replace(SixthProblem.java:40) at SixthProblem.sumofMultiple(SixthProblem.java:34) at SixthProblem.main(SixthProblem.java:57) – tommy123 Dec 30 '19 at 04:12
  • That exception is not due to printing the array. I recommend you read: https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Jacob G. Dec 30 '19 at 04:14
  • I cannot understand what the OP is trying to mean? – tommy123 Dec 30 '19 at 04:16
  • 1
    Ok got it i took arr.length intially. I had to take arr.length - 1 – tommy123 Dec 30 '19 at 04:19
  • @tommy123 Did this fix your issue? – Jacob G. Dec 30 '19 at 04:59