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 2 of 2

Thread: Program not returning expected value.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program not returning expected value.

    The code assignment is as follows:

    /**
    * Determine if there is a majority element in an array of ints.
    * The parameter list is not altered as a result of this
    * method.
    * @param list != null
    * @return the first index of the first occurrence of the majority element if it exists.
    * If a majority element does not exist return -1.
    */

    with my test cases, it keeps returning -1, it does not seem to even use the if loops. Can anyone tell me why it is doing that?

    public static int findMajority(int[] list) {
            assert ( list != null ) : "Violation of precondition: findMajority";
            int count = 0;
            int max = (list.length / 2) + 1;
            int index = -1;
            for ( int i = 0; i < list.length;i++){
            	for ( int k =list.length-1; k <= 0;i--){
            		if (list[i] == list[k]){
            			count++;
            		if ( count >= max){
            			index = i;
            			return index;
            		}
            		}
            	}
            }
    		return index;
        }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Program not returning expected value.

    for ( int k =list.length-1; k <= 0;i--)
    this line will be always false.


    k>=0
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 02:50 PM
  2. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:33 PM
  3. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM
  4. ';' expected?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2009, 11:55 AM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM