Results 1 to 8 of 8
  1. #1
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Smile two dimensional array print response

    I know this is against the rules but it keeps logging me out when I enter that thread for some reason.

    Anyway to the point your problem is here
    for (int i = 0; i< DAYS.length; i++) {
    System.out.print(DAYS[i]); }

    instead you want to initialise a variable outside the whole loop, then add one to i without the for loop inside.

    So this is how I would do it

    public void printTable(){
    int start, end, count;
    end = 16;
    int i = 0;
    count = 0;
    System.out.println();
    System.out.println("-----------------------");
    for (start = 9; start <= end; start++){
    System.out.print(start + " ");
    }
    for (int row = 0; row < timetable.length; row++){
    System.out.println("");
    for (int col = 0; col < timetable[row].length; col++){
    System.out.print(timetable[row][col] + " ");
    }
    System.out.print(DAYS[i]);//soz I thought it was an array list :/
    i++;

    }
    System.out.println();
    System.out.println("-----------------------");
    }

    Hope this helps :)

    To explain what I did:
    What you did was you put a for loop for the days inside the large loop, therefore all the contents of the arraylist will be printed every time

    What I did was made it so that i would add 1 to i every time, instead of going through the whole arraylist every run of the loop. This way every time it will run the corresponding day will print.

    -Bayan-
    Last edited by bayan; 04-27-2010 at 06:45 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If I'm correct, do you want to increase each element in the array by one and print the result?

  3. #3
    Myles is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much :)

    I had this error
    cannot find symbol method get(int)

    from this line
    Java Code:
    System.out.print(DAYS.get(i));
    I have not used "get" before, so i changed it to
    Java Code:
    System.out.print(DAYS[i]);
    Should I not have done that? It looks like it's working the same either way..

    Anyway, that you very much for your help :) It now seems so obvious...

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Is that DAYS is an ArrayList or just an array. get(int index) use to retrieve elements in an ArrayList, not in array? Did you know the difference between those two ?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Myles View Post
    TAnyway, that you very much for your help :) It now seems so obvious...
    Actually you pick the wrong way to solve address this.

  6. #6
    Myles is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    oh its an Array not arrayList.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Smile

    Np, just remember to keep on the lookout for things like this.

    I'm just like you I get stuck on these things a lot it takes me a long time to debug....

    -Bayan-

Similar Threads

  1. 2 dimensional array help!
    By ber1023 in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 01:29 AM
  2. two dimensional array print
    By Myles in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 12:10 PM
  3. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM
  4. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 04:34 PM
  5. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:48 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
  •