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

Thread: Help with method. Finding minimum value in Array

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with method. Finding minimum value in Array

    So I trying to find the minimum value in the scoreboard array. I tried this

    public int minValue(){// Finds the minimum value value in the array scoreboard 
        int x=0;
        for(int j=0;j<13;j++){
          int z=scoreboard[j];
          if(scoreboard[j] < z)
            x=scoreboard[j];
        }
        return x;
    }

    But it always returns zero, any help?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Help with method. Finding minimum value in Array

    int z=scoreboard[j];
    if(scoreboard[j] < z)
    You also need to carefully examine your code.
    Lets say that the first element in the array(j is 0) is 10. That first line of code assigns 10 to z. You then ask if 10 < 10 which is never going to be true. Therefore the value of x never changes.

    BTW please use meaningful variable names.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with method. Finding minimum value in Array

    so something like this...

    public int minValue(){// Finds the minimum value value in the array scoreboard 
        int smallest=scoreboard[0];
        for(int j=0;j<13;j++){
          if(scoreboard[j] < smallest)
            smallest=scoreboard[j];
        }
        return smallest;
      }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Help with method. Finding minimum value in Array

    What happened when you tried it? Make sure you test it with several arrays containing a variety of values.

    BTW hard coding the value 13 is not a good idea. Use the length of the array instead.
    Improving the world one idiot at a time!

Similar Threads

  1. Problem with finding value from array
    By Alexryu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 10th, 2013, 02:15 AM
  2. Finding the lowest number in an array.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 01:49 AM
  3. finding and deleting from an array?
    By 93tomh in forum Java Theory & Questions
    Replies: 19
    Last Post: August 13th, 2012, 09:39 AM
  4. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 09:35 PM