Results 1 to 5 of 5
  1. #1
    josho493 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Why isn't this snipet of code working correctly

    I have a calculate max & calculate min class. The maximum class is working just fine, but the minimum one is always returning 0, even though 0 is not a value going through it. Is there something wrong?

    Java Code:
    public static int calculateMin(int data[]){
    	int min = 50*50; 
    		
    	for(int i = 0; i < data.length; ++i){
    		min = Math.min(min, data[i]);
    	} // end for
    		
    	return min;
    } // end calculateMin

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,748
    Rep Power
    19

    Default Re: Why isn't this snipet of code working correctly

    Print out the values in the data array as you go around that loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why isn't this snipet of code working correctly

    Possibly there is a zero in the data array. You can check using System.out.println():

    Java Code:
    for(int i = 0; i < data.length; ++i){
        if(data[i] == 0) {
           System.out.println("data[" + i + "] is zero");
           min = Math.min(min, data[i]);
        }
    }
    But without seeing some runnable code that's only a guess.

  4. #4
    josho493 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Why isn't this snipet of code working correctly

    Yeah sorry, my bad. The problem was elsewhere in my code but causing problems here. Thanks for your help

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why isn't this snipet of code working correctly

    edit: way too slow again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with If/Else not working correctly?
    By kalaiss in forum New To Java
    Replies: 8
    Last Post: 04-16-2012, 06:32 AM
  2. method not working correctly
    By r1b in forum New To Java
    Replies: 4
    Last Post: 01-04-2012, 08:09 PM
  3. Applet not working correctly?
    By Beavotropper2 in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 06:32 AM
  4. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 03:01 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •