Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Problem with arrays2

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.
    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;
    	}
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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."

    Instead, you want it to read:
    "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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with arrays2

    Quote Originally Posted by Jad_Asmar View Post
    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.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with arrays2

    Quote Originally Posted by Jad_Asmar View Post
    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:

    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?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays2

    Ok i got it all right now but i do have another question , take a look :
    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?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with arrays2

    How can the isSorted() method return true after only looking at ONE pair of values in the array?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Jad_Asmar (December 5th, 2013)

  9. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with arrays2

    Quote Originally Posted by Jad_Asmar View Post
    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.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  10. The Following User Says Thank You to andbin For This Useful Post:

    Jad_Asmar (December 5th, 2013)

  11. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays2

    I get it , thank you guys for your help

  12. #10
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with arrays2

    Quote Originally Posted by Jad_Asmar View Post
    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.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  13. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays2

    you just switch the "returns" anyhow here's the code :
    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;
    	}

  14. #12
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with arrays2

    Ok, it's correct.
    If you want you can test i < a1.length, which is conceptually the same just more concise (personally I prefer).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  2. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  3. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  4. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM