Hello, thanks for taking the time to read this.

I'm working on (what should be) a simple, simple java program, I am very new to java so please bear with me.
I'm writing code with a method that takes an array of int values and determines if all the numbers are different from each other, that they are distinct.

Here is what I have so far:

``````public class Array
{
public static void main(String[] args)
{
int[] numbers = {5, 10, 15, 20, 25, 1, 2};
System.out.println("Checking array to determine if all numbers are different: " +
compareValues(numbers));

}

public static boolean compareValues(int[] numArray)
{
for(int i = 0; i < numArray.length; i++)
{
for(int j = 0; j < numArray.length; j++)
{
if((numArray[i] == (numArray[j])) && (i != j))
{
return true;
}
}
}
return false;
}
}
``````

My code compiles, but the result I get is false... when it should be true (because all the values ARE distinct). I feel like its all wrong!
@compareValues, here is where I am stuck. I am having much difficulty in creating for-loops. Am I using 1 to many for-loops? Should I be using for-loops?

If anyone could help and explain their reasons it would be very much appreciated!

## All 9 Replies

Yes, for loops would be the ones to use.
Can you explain what your logic for solving the problem is? You should work out the logic to use before writing any code. List the steps the code should take to solve this problem.

I am not sure that a newbie would understand what logic in solving a problem even though the problem is trivia...

Anyway, the problem lies in the for-loop condition and how you need to break out of the loop to return a value.

Let's talk about the loop first. You need to check each element against one another but itself. Each element has its own position. The position can be count from 0 up to n-1 where n is the total number of elements.

Using your own example `int[] numbers = {5, 10, 15, 20, 25, 1, 2};`, there are 7 elements in this value, so the position goes from 0 up to 6. You may try to think about step by step before you apply nested loops in your program.

If you think that when you compare element 0 against element 1, you won't need to compare element 1 against element 0 again, correct? Now, the comparing will become linear because you would need to check all elements from position 1 to 6 against element 0. Then you would need to check all elements from position 2 to 6 agains element 1. And this will keep going on until you have only element at position 5 to be checked against position 6.

``````/*
i.e.
elem0 <- element 0
loop through from element 1 to element 6
compare each value with elem0
now done with element 0
elem1 <- element 1
loop through from element 2 to element 6
compare each value with elem1
now done with element 1
...
elem5 <- element 5
loop through from element 6 to element 6 (just to be uniform with others)
compare each value with elem5
now done with element 5

no more to be compared, done comparing
*/
``````

One you understand the loop concept, then you may add another loop around the loop you already created. That's called nested loop. You see that the starting value for the inner loop is always greater than the compared element by 1, right? That's the trick to keep away the comparison of the same position. So if the outer loop has whatever value, the inner loop should start at least from the (outer value + 1).

Next, how to determine the return value. Let say you want to return true if none of element is duplicated. Now ask yourself a question "Do I need to compare every pair before I know that there is no duplicated?" If the answer is yes, then the return value should be the last value you will return. In other words, the opposite value (false) should be returned right away if you find a duplicated. If the answer is no, then it will be in reverse from the prvious sentence.

The answer should be "yes" and that mean your current loop is comparing it wrong. It will always return true if the first and the second elements are different value. What you need to do is to reverse it as I explained above (instead of return true, you need to reverse the condition check of the if-statement and return false). If everything goes well (no duplicated element), it will return true at the end because it has compared every possible pairs and found no wrong.

Hope this help...

I am not sure that a newbie would understand what logic in solving a problem

How can anyone write a program without thinking through the steps needed to solve the problem?
Coding before deciding what the code is supposed to do can be a big waste of time.

Nice post BTW

This is a good example of why variable and method names are important. With a method called "compareValues" it's totally unknown what returning true or false will mean. Change the name to allValuesAreDifferent and the fact that the logic has true and false the wrong way round jumps straight out at you...

``````boolean allValuesAreDifferent(...
...
if (numArray[i] == numArray[j]) return true;  // obviously wrong
``````

How can anyone write a program without thinking through the steps needed to solve the problem?

It is simple. :) Those who understand what's going on already will not feel disconnected about what to do. Those who don't understand will feel disconnected and don't know how or what to ask. The program does not really reflect the logic of problem solving but to put something out and hope it will work. In other words, there is no logic of solving a problem but rather a list of steps that one thinks it would work.

there is no logic of solving a problem but rather a list of steps that one thinks it would work.

I guess there is a terminology problem. The list of steps, the logic and the algorithm are different ways of describing the same thing: What needs to be done to solve the problem?

put something out and hope it will work

That's the approach taken when there hasn't been any thought on the steps needed to solve the problem. Coding before designing.
I try to encourage OPs to think out how to solve the problem manually, come up with the list of steps to be done. When the logic is good, then write the code.

just start your j loop with i+1.And then see the result.
I hope its working.

Thank you all for the help! I realize I am getting mixed up on the return true/false statements, and that was in part to my bad method naming.
I have redone my code by thoroughly reading and understanding Taywin's post. Thanks a bunch! I have also added explanations to show why I am doing what I do.
Also, after much reading and coding, I finally understood jalpesh_007's suggestion, so I have incorporated both into my code.

Have I correctly done my return true/false statements?

``````public class Array
{
public static void main(String[] args)
{
//Initialize array
int[] numbers = {5, 10, 15, 20, 25, 1, 2};

System.out.println("Yes = True || No = False");
System.out.println("Are any values the same?: " +
anyRepeatValues(numbers));

System.out.println("Are all the values in the array different?: " +
allValuesAreDifferent(numbers));

}

public static boolean anyRepeatValues(int[] arrayNum)
{
//Start loop at element0, loop through from element 1 to 6
for(int a = 0; a < arrayNum.length; a++)
{
//Compare each value with element0
//If element at position [a] = element position 
//Then return false, because there are no repeating values
if(arrayNum[a] == arrayNum)
return false;

for(int b = 1; b < arrayNum.length; b++)
{
if(arrayNum[b] == arrayNum)
return false;

for(int c = 2; c < arrayNum.length; c++)
{
if(arrayNum[c] == arrayNum)
return false;

for(int d = 3; d < arrayNum.length; d++)
{
if(arrayNum[d] == arrayNum)
return false;

for(int e = 4; e < arrayNum.length; e++)
{
if(arrayNum[e] == arrayNum)
return false;

for(int f = 5; e < arrayNum.length; f++)
{
if(arrayNum[f] == arrayNum)
return false;
} //end f loop
} //end e loop
} //end d loop
} //end c loop
} //end b loop
} //end a loop
//Return true if there are repeating values
return true;
} //end areThereReaptingValues

public static boolean allValuesAreDifferent(int[] numArray)
{
//Start loop at element0, loop through from element 1 to 6
for(int i = 0; i < numArray.length; i++)
{
//Increase each element position by 1, while looping through 1 to 6, then
//2 to 6 etc.
for(int j = i + 1; j < numArray.length; j++)
{
//If the element at position [i] is equal to the element at position [j]
//return false because the values are not different
if(numArray[i] == (numArray[j]))
return false;
}
}
//Return true if all the values are different
return true;
}
}
``````

It is a good start for you. Keep practicing and you will be able to optimize the code later. Remember, do it correctly is the first priority in coding. Optimization comes later once you are familiar to what you are doing.