Results 1 to 4 of 4
  1. #1
    Veronica91 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Return True for all Positive intgers in an Array, false otherwise

    Hello everyone, I'm trying to teach my self Java but I don't know how to return an array value. I simply would like the console to return true for all entered integers for an array method that are positive and false if they are negative.

    Thanks for reading.

    Java Code:
    public class ArrayPositiveInts {
    
    	public static boolean allPositve (int[] array) 
    	{	
    int result = 0;
    		for (int i = 0; i < array.length; i++) 
    		{		
    			result = array[i];
    			if (i % 2 == 0)			
    				return true;
    			else if (i < 0)			
    				return false;
    		}
    	}
    	
    	public static void main(String[] args) 
    	{		
    		boolean result = allPositive(-1,2,3);
    	}
    }

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

    Default Re: Return True for all Positive intgers in an Array, false otherwise

    You get compilation errors, right? Read them. they're useful; if you don't understand them you can post them here and we'll try to help you out.

    kind regards,

    Jos

    ps. your allPositive( ... ) method expects an array of ints, not just three int parameters ...
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Veronica91 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: Return True for all Positive intgers in an Array, false otherwise

    I fixed the original code I just need to know how to pass the parameter value for int[] myArray to the main, I have tried everything I know.

    Java Code:
    public class ArrayPositiveInts {
    
    	public  boolean allPositve(int[] array) {
    
    		for (int i = 0; i < array.length; i++) {
    
    			if (array[i] <= 0) 
    				return true;			
    			 else
    				return false;
    			}
    		}	
    
    	public static void main(String[] args) {			
    		
    		boolean results = ArrayPositiveInts.allPositive(?????);		
    	}
    }

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Return True for all Positive intgers in an Array, false otherwise

    For the allPositve method you can do this: allPositve(int... numbers)
    Not really sure what this is called, but it works.

    Use it like this:
    Java Code:
    public class ArrayPositiveInts {
    
    	public static boolean allPositve(int... numbers) {
    		
    		for(int number : numbers){
    			if (number <= 0) 
                    return true;            
                 else
                    return false;
                }
    		return false;
    	}
    
    	public static void main(String[] args) {
    
    		boolean results = ArrayPositiveInts.allPositve(50, 60, 70, 99, -500);
    	}
    }
    If you want to return an array of booleans for the results, you can use an ArrayList in the allPositve method and instead of returning true or false, you can add a new item into the arraylist and then to return an array you can use the toArray function for the array list.
    Last edited by PhQ; 09-09-2012 at 04:48 PM.

Similar Threads

  1. Replies: 5
    Last Post: 03-30-2012, 08:53 PM
  2. Replies: 2
    Last Post: 05-25-2011, 02:33 PM
  3. Return positive values only
    By DomBrown in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 07:48 PM
  4. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  5. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 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
  •