2

I'd need some help with this. I'm new to java and this is my task:

I have to write code that has an integer array in it. A pair of the arrays values have to be =42. Then I have to output the pair through normal System.out.println.

This is the code I've got so far. Maybe I even got this completely wrong. My problem is getting the program to correctly output what the pairs are that are =42.

public class Main {
    public static void main(String[] args) {
        int[] array = { 2, 3, 5, 6, 8, 6, 5, 39, 40, 34 };
        for (int i = 0; i < array.length; i++) {
            for (int k = 0; k < array.length; k++)
               if (array[i] + array[k] == 42)
                   System.out.println("Found pairs at");
        }
    }
}
4
  • 1
    what is the problem with your code?
    – Wasi Ahmad
    Commented Nov 26, 2017 at 12:09
  • 1
    Welcome to SO! to get the most help please share what have you tried , show example input & output for the desirable solution and what problem have you encounter
    – Mzf
    Commented Nov 26, 2017 at 12:10
  • i have tried to make an output that says something like "2 and 40 equals 42"
    – Nithux
    Commented Nov 26, 2017 at 12:11
  • Basically I want it to be able to show which numbers of the array equal 42
    – Nithux
    Commented Nov 26, 2017 at 12:12

2 Answers 2

1

You're almost there :) You can build the output string simply by using the + operator:

public class Main {
    public static void main(String[] args) {
        int[] array = {2,3,5,6,8,6,5,39,40,34};

        for (int  i=0; i< array.length  ; i++) {
            for (int k=0; k< array.length; k++) {
                if (array[i] + array[k] == 42)
                    System.out.println("Found pairs at " + i + " " + k + ": " + array[i] + " and " + array[k] + " equals 42");
            }
        }
    }
}

Alternatively, you could use the format function, which is probably easier to read:

System.out.format("Found pairs at %d %d : %d and %d equals 42", i, k, array[i], array[k]);
1
public static void main (String[] args) throws java.lang.Exception
{
    int[] array = {2,3,5,6,8,6,5,39,40,34};
    for (int i=0; i<array.length; i++){
        for (int k=0; k<array.length; k++)
           if (i != k && array[i] + array[k] == 42)
               System.out.println("Found pairs at " + i + " and " + k + ".");
    }
}

Output:

Found pairs at 0 and 8.
Found pairs at 1 and 7.
Found pairs at 4 and 9.
Found pairs at 7 and 1.
Found pairs at 8 and 0.
Found pairs at 9 and 4.

See a live demo.


Edit: I have added i!=k in the if condition because you are looking for two numbers in the array that sum to 42. So, if the number 21 appears in the input array, your program will give you the index of number 21 twice when i=k since you are checking array[i] + array[k] == 42.

1
  • Can you explain that condition? I think that helps OP more than just the code.
    – Zabuzard
    Commented Nov 26, 2017 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.