The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Array Problem

Removing "|| scores[i] == scores [i+1]" ?

That does not work. The output is still: true false. Besides, I need it to complete the assignment (return true if each score is equal).

I know what seems to be the problem, however. The for loop. It compares 3 with 1 and 2 with 3, like you said. The question is...how can I make it so it only checks the numbers and not put the "true or false" until the last two numbers of the array has been checked.

Re: Array Problem

You must iterate through the entire array to compare the elements. When you call a "return" you are returning a value to the method which called the method. In this instance as haski has mentioned there is no need for all the to return true unless it has iterated through the array and met the conditions.

The way you have setup your loop is good. When you check the conditions you must meet you can see something. (a > b and a == b) = true, but if you return something here then you do not compare the rest of the values. There is a condition you can setup in the if statement that would justify the return of a boolean inside the loop and return the correct answer.

Conditions:
1. (a > b) = true;
2. (a == b) = true;
3. (???) = false

Also make sure to remove the else portion in the if statement. As mentioned previously as soon as a return is called it returns a value to the method that called it and doesn't run the rest of the loop.

Re: Array Problem

Sorry to bump this, but I really want to know how to make the code work. :/

Basically there is a logical problem in the flow of execution. Let me break it down with pseudocode.

for each element in the array
if element is less than or equal to the next element
return true
else
return false

Can you see the problem? It isn't checking that this 'less than or equal to the next element' is true for EVERY element. Only the first one. The return statement bails out the loop and returns either true or false.

What you need is a temporary variable, something to hold the truthfulness of the statement and give it a chance to check that the entire array is in increasing order.

temp = false
for each element in the array
if element is less than or equal to the next element
temp = true
else
return false
end loop
return temp

Note that if any any element is greater than the next the entire function stops and returns false. There is no need to continue iterating through the array because it can no longer be entirely increasing. Abusing this bit of logic we can further simplify the pseudocode.

for each element in the array
if element is greater than the next element
return false
end loop
return true

This is the most elegant solution (haski mentioned it) because you need to completely prove the truthfulness whilst a single false disproves.

Re: Array Problem

Basically there is a logical problem in the flow of execution. Let me break it down with pseudocode.

for each element in the array
if element is less than or equal to the next element
return true
else
return false

Can you see the problem? It isn't checking that this 'less than or equal to the next element' is true for EVERY element. Only the first one. The return statement bails out the loop and returns either true or false.

What you need is a temporary variable, something to hold the truthfulness of the statement and give it a chance to check that the entire array is in increasing order.

temp = false
for each element in the array
if element is less than or equal to the next element
temp = true
else
return false
end loop
return temp

Note that if any any element is greater than the next the entire function stops and returns false. There is no need to continue iterating through the array because it can no longer be entirely increasing. Abusing this bit of logic we can further simplify the pseudocode.

for each element in the array
if element is greater than the next element
return false
end loop
return true

This is the most elegant solution (haski mentioned it) because you need to completely prove the truthfulness whilst a single false disproves.

Re: Array Problem

Your result (temp) keeps getting overwritten with each loop, so what will happen is temp will really only be set by the last comparison (3 versus 7 in this case). You might want to initialize temp with true and assign it as temp=temp&true and temp=temp&false (the latter of course can just be temp=false).

Re: Array Problem

Originally Posted by jpharte

Your result (temp) keeps getting overwritten with each loop, so what will happen is temp will really only be set by the last comparison (3 versus 7 in this case). You might want to initialize temp with true and assign it as temp=temp&true and temp=temp&false (the latter of course can just be temp=false).

Okay, I understand the reasoning behind it, but I do not understand the &true thing. What is that? I haven't seen that before.