Results 1 to 8 of 8
  1. #1
    Zarquad is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Array Syntax help.

    How do I give a whole array as an argument to a method? This is my code:

    Java Code:
    class Displayer {
    	public static boolean areFactors (int n, int i[]) {
    		for (int count = 0; count <= i.length; count++) {
    			if (n%i[count] != 0) {
    				return false;
    			}
    		}
    		return true;
    	}
    	
    	public static void main (String[] args) {
    		int[] i;
    		i = new int[3];
    		i[0] = 10;
    		i[1] = 50;
    		i[2] = 25;
    		System.out.println (areFactors(100, i[]));
    	}
    }
    Also, just to make sure, once a return statement is run the rest of the method terminates even if there is still code left, right?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Array Syntax help.

    First question - leave out the square backets. However, assuming you are trying to print it out in the print method, that will not work. It only prints a cryptic description of the reference. You need to index thru the array to print it out. Second question - right

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Array Syntax help.

    If you work with arrays check this out as well: Arrays (Java Platform SE 6)
    Many useful tools

  4. #4
    Zarquad is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Array Syntax help.

    Quote Originally Posted by jim829 View Post
    First question - leave out the square backets. However, assuming you are trying to print it out in the print method, that will not work. It only prints a cryptic description of the reference. You need to index thru the array to print it out. Second question - right

    Regards,
    Jim
    I'm trying to print the boolean value that is returned. And removing the square brackets didn't work. It messed up the i.length because it doesn't see i as an array. :/


    superhaNds, I'm just going through a textbook. The chapter I'm on right now is about arrays.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Array Syntax help.

    He didn't mean that. When you pass it as an argument, you just type the name of the variable areFactors(100, i);

  6. #6
    Zarquad is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Array Syntax help.

    Java Code:
    class Displayer {
    	public static boolean areFactors (int n, int i[]) {
    		for (int count = 0; count <= i.length; count++) {
    			if (n%i[count] != 0) {
    				return false;
    			}
    		}
    		return true;
    	}
    	
    	public static void main (String[] args) {
    		int[] i;
    		i = new int[3];
    		i[0] = 10;
    		i[1] = 50;
    		i[2] = 25;
    		System.out.println (areFactors(100, i));
    	}
    }
    I'm getting this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Displayer.areFactors(Displayer.java:4)
    at Displayer.main(Displayer.java:17)

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Array Syntax help.

    count should be < i.length , otherwise the index would go to [3] which doesn't exist.

  8. #8
    Zarquad is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Checking DTD syntax...
    By xlomo in forum XML
    Replies: 0
    Last Post: 10-01-2011, 03:14 AM
  2. Syntax Errors
    By andimiami in forum New To Java
    Replies: 8
    Last Post: 09-15-2011, 02:23 PM
  3. Syntax error when returning an array
    By wantakill in forum New To Java
    Replies: 2
    Last Post: 09-03-2010, 07:39 PM
  4. Syntax question
    By rockeater in forum New To Java
    Replies: 3
    Last Post: 09-01-2010, 12:52 AM
  5. Syntax in eclipse
    By insectincest in forum Eclipse
    Replies: 1
    Last Post: 04-12-2010, 12:10 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
  •