# Problem with arrays2

• December 4th, 2013, 02:08 PM
Problem with arrays2
I am making a program that if the user enter a sequence of double in an array if it is sorted right the prog will return true if not it will return false .. what is wrong here ? always i get true.
Code java:

```public class test2 { public static void main(String[] args) { double[] crazy = { 1.0, 10.0, 11.0, 50.0, 12.0, 13.0 }; System.out.println(isSorted(crazy)); }   public static boolean isSorted(double a1[]) { for (int i = 1; i <= a1.length; i++) { System.out.println(a1[i]); System.out.println(a1[i]); if (a1[i - 1] < a1[i]) { return true;   }   }   return false; } }```
• December 4th, 2013, 02:17 PM
aussiemcgr
Re: Problem with arrays2
Well, if your loop ever finished, you would probably be getting an index out of bounds exception, because a1[a1.length] is not a valid index. Array indexes range from 0 to the length-1.
But, that is one of several issues with your code.
The reason you always get true is because your current code reads:
"If ANY two numbers are in the correct order, return true. If NONE of them are in the correct order, return false."

"If ALL of the numbers are in the correct order, return true. If ANY of them are not in the correct order, return false."

Do you understand why I said those statements, given your current code?
If you do understand that much, try and see if you can figure out how to change your code to reflect the way it should read. If you are not sure how, I'll be happy to walk you a little bit further through it.
• December 4th, 2013, 02:23 PM
andbin
Re: Problem with arrays2
Quote:

I am making a program that if the user enter a sequence of double in an array if it is sorted right the prog will return true if not it will return false .. what is wrong here ? always i get true.

First, the index variable i can reach the value of a1.length which is an index out of bounds. Thus, a1[i] will throw an exception when i equals a1.length.
Second (apart the first problem), your actual isSorted method returns false to indicate that the array is in descending order and returns true otherwise. The true doesn't say that the array is "ordered" (not even ascending)!
You should review the concept.
• December 4th, 2013, 02:25 PM
Re: Problem with arrays2
Thank you for your help guys but i'm sorry i still am a beginner to java can you explain more to me? And help me to solve my problem ? Thank you.
• December 4th, 2013, 02:33 PM
andbin
Re: Problem with arrays2
Quote:

Thank you for your help guys but i'm sorry i still am a beginner to java can you explain more to me? And help me to solve my problem ? Thank you.

You are testing:

Code :

```if (a1[i - 1] < a1[i]) { return true; }```
This is right itself. But this means that only by the first occurrence of a1[i-1] is-lower-than a1[i] it returns true, ignoring the rest. This doesn't mean that the entire array is in ascending order!

You want to verify that the entire array is in ascending order. You can know this only if all pairs are ordered. This means that the return true must be at the end, after the for, and the test ..... think: when is not in ascending order?
• December 4th, 2013, 06:03 PM
Re: Problem with arrays2
Ok i got it all right now but i do have another question , take a look :
Code java:

```public class test2 { public static void main(String[] args) { double[] crazy = { 1.0, 2.0, 2.5 }; System.out.println(isSorted(crazy)); }   public static boolean isSorted(double a1[]) { for (int i = 1; i <= a1.length - 1; i++) { if (a1[i - 1] > a1[i]) { return true; }   }   return false; } }```
now the fault here is that i should switch the return : the first return should be false and the next one should be right , however my question is why in this case i always get a false ? why the opposite is true but not like now?
• December 4th, 2013, 07:37 PM
Norm
Re: Problem with arrays2
How can the isSorted() method return true after only looking at ONE pair of values in the array?
• December 5th, 2013, 02:03 AM
andbin
Re: Problem with arrays2
Quote:

the first return should be false and the next one should be right , however my question is why in this case i always get a false ? why the opposite is true but not like now?

Think carefully about the following: the entire array is in ascending order only if all pairs are in ascending order. It's sufficient one (anywhere) pair not in ascending order to state immediately that the entire array is not in ascending order.
• December 5th, 2013, 04:43 AM
Re: Problem with arrays2
I get it , thank you guys for your help :)
• December 5th, 2013, 05:03 AM
andbin
Re: Problem with arrays2
Quote:

I get it , thank you guys for your help :)

Ok, please. You can post your, hope right, code. It could be useful to others and I can tell you if it's correct.
• December 5th, 2013, 07:55 AM
```public class test2 { public static void main(String[] args) { double[] crazy = { 1.0, 2.0, 2.5 }; System.out.println(isSorted(crazy)); }   public static boolean isSorted(double a1[]) { for (int i = 1; i <= a1.length - 1; i++) { if (a1[i - 1] > a1[i]) { return false; }   }   return true; }```