Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: 2D Array help

  1. #1
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 2D Array help

    Hey, I was wondering why this code isn't working. It's just printing random letters and numbers out instead.
    I want for it to print all the days in order throughout the year, for example:
    1
    |
    31
    (February is excluded)
    1
    |
    31
    1
    |
    30
    ...and so on...

    Here's the code:
    Java Code:
    public class b4 {	
    	public static void main(String[]args){
    		for(int[] x: day()){
    			System.out.println(x);
    		}
    	}
    	
    	public static int[][]day(){
    		int[]jan = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		//I'm skipping February for now, I know how to get the leap/non-leap year but for now I'll just skip it.
    		int[]mar = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		int[]apr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
    		int[]may = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		int[]jun = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
    		int[]jul = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		int[]aug = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		int[]sep = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
    		int[]oct = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		int[]nov = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
    		int[]dec = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    		
    		int[][]day = {jan, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    		return day;
    	}
    	
    	//Ignore this 
    	public static int[]feb(){
    		int[]feb = {};
    		return feb;
    	}
    }
    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    It's just printing random letters and numbers out instead.
    Please post what the program is printing out.

    Are you talking about the default value returned by the Object class's toString method for an array?
    x is an array, not an single value

    To format the contents of an array for printing see the Arrays class's toString() method
    Last edited by Norm; 03-16-2012 at 12:46 AM.

  3. #3
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Here's what the program prints out:

    [I@316becfe
    [I@2494310b
    [I@7d1fdbef
    [I@36a06816
    [I@5d11985e
    [I@2e2a730e
    [I@64e7b3cf
    [I@27ce0eca
    [I@492d0430
    [I@2a6e10d8
    [I@2013eaab

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    That is from the toString method. See post#2

  5. #5
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Where would i put it?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    Where do you want to format the contents of the array referenced by x for printing?

  7. #7
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    I guess in the method where i declared the arrays.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    x is the array you are trying to print.

  9. #9
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    I'm totally lost

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    x is an array. You will not see its contents when you try to print it with println. You get the String returned by the Object class's toString method.
    You need to treat x as an array and access its members by indexing it: for example x[0] would give you the first element of the array.

    Did you read the API doc for the Arrays class's toString method?

  11. #11
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Yes i just did, after i saw your reply, but i don't really understand it. Its alright.

    would i just do something like:

    return day().toString;

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    return day().toString;
    What happened when you tried that?

    if x were defined like this:
    int[] x = {1,2,3};
    How would you print out its contents?

    Write a small program to print it. Try this:
    System.out.println(x);
    Then write a loop to print the elements one by one.

  13. #13
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Ok, heres the code that I ended up with after that:
    Java Code:
    public class b4 {   
        public static void main(String[]args){
                System.out.println(day());
        }
         
        public static String day(){
            int[]jan = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            //I'm skipping February for now, I know how to get the leap/non-leap year but for now I'll just skip it.
            int[]mar = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            int[]apr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
            int[]may = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            int[]jun = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
            int[]jul = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            int[]aug = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            int[]sep = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
            int[]oct = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
            int[]nov = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
            int[]dec = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
             
            int[][]day = {jan, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
            return day.toString();
        }
         
        //Ignore this 
        public static int[]feb(){
            int[]feb = {};
            return feb;
        }
    }
    And here's what it printed:

    [[I@19117681


    And now i'll try what you just said.
    EDIT:::
    if x were defined like this:
    int[] x = {1,2,3};
    How would you print out its contents?

    Write a small program to print it. Try this:
    System.out.println(x);
    Then write a loop to print the elements one by one.
    And heres how i would do that:
    Java Code:
    public class b5 {
    	public static void main(String[]args){
    		int[]x = {1,2,3};
    		for(int y: x){
    			System.out.println(y);
    		}
    	}
    }
    Last edited by Etimer; 03-16-2012 at 02:04 AM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    If you want day() to return a String that prints like this:
    1
    2
    3
    etc
    Then you will need to have loops that go through each of the arrays in the day() method and build a long String that looks like this:
    "1\n2\n3\netc"

  15. #15
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    And i would put that in the parameters for the toString() function?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    If the day() method returns a String.
    You would build a String and return it:
    String aStr "1\n..."; // some how build the String you want
    ..
    return aStr; // return the String to the caller of the day() method

  17. #17
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    so i wouldnt even be using the day array at all?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    Use the logic for printing the contents of array that you posted in #13
    in the code in post#1 to print the contents of the array x.

  19. #19
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    That wouldn't work because it's a 2d array and it cant just loop through for some reason.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: 2D Array help

    so i wouldnt even be using the day array at all?
    How do you want to do it?
    Have day() return a String formatted for printing
    or have it return a 2D array of ints that have to be formatted for printing?

Page 1 of 2 12 LastLast

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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