Results 1 to 16 of 16

Thread: 2-D array help

  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default 2-D array help

    I have a project due thursday and it involves creating a 2d array that displays the output

    123
    456
    789

    Here is what i have so far...

    public class twodarray {

    public static void main(String[] args) {
    int [][]T={{1,2,3},{4,5,6},{7,8,9}};
    list (T);

    public class list (int[]x)
    {
    for (int R=0; R<3;R++)
    for (int C=0; C<3;C++)
    System.out.print(x[R][C] + " ");


    }
    }

    }

    i also have to make arrays such as just

    blank blank blank
    blank 5 blank
    blank blank blank

    but it appears that that has to do with changing list to left diag or right diag but i can't seem to display the list...

    what am i doing wrong?
    thanks in advance

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is all sorts wrong with that. I don't believe you meant to make list a class, it should be a static method for now.

    change
    Java Code:
    public class list(int[]x)
    to
    Java Code:
    public static void list(int[]x)
    Other than that the code seems fine.

  3. #3
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    There is all sorts wrong with that. I don't believe you meant to make list a class, it should be a static method for now.

    change
    Java Code:
    public class list(int[]x)
    to
    Java Code:
    public static void list(int[]x)
    Other than that the code seems fine.
    i did that and it got rid of a few errors but replaced them with new ones...

    F:\Java CSCL-120-W01-Programming\Project\Project3.java:17: illegal start of expression
    public static void list(int[]x)
    ^
    F:\Java CSCL-120-W01-Programming\Project\Project3.java:17: illegal start of expression
    public static void list(int[]x)
    ^
    F:\Java CSCL-120-W01-Programming\Project\Project3.java:17: ';' expected
    public static void list(int[]x)
    ^
    F:\Java CSCL-120-W01-Programming\Project\Project3.java:17: '.class' expected
    public static void list(int[]x)
    ^
    F:\Java CSCL-120-W01-Programming\Project\Project3.java:17: ';' expected
    public static void list(int[]x)
    ^
    5 errors

    i don't get it :confused:

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are declaring the method inside main, use a closing curly brace to end main before writing list.

  5. #5
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    There is all sorts wrong with that. I don't believe you meant to make list a class, it should be a static method for now.

    change
    Java Code:
    public class list(int[]x)
    to
    Java Code:
    public static void list(int[]x)
    Other than that the code seems fine.
    Quote Originally Posted by sunde887 View Post
    You are declaring the method inside main, use a closing curly brace to end main before writing list.
    ok so i think this is what you meant...

    public class twodarray {


    public static void main(String[] args) {
    int [][]T={{1,2,3},{4,5,6},{7,8,9}};
    }
    list (T);


    public static void list (int[]x)
    {
    for (int R=0; R<3;R++)
    for (int C=0; C<3;C++)
    System.out.print(x[R][C] + " ");


    }
    }

    and it did help a lot i am down to just two errors...

    F:\Java CSCL-120-W01-Programming\Project\Project3.java:16: invalid method declaration; return type required
    list (T);
    ^
    F:\Java CSCL-120-W01-Programming\Project\Project3.java:16: <identifier> expected
    list (T);

    thank you so much already guys i really do appreciate this

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Everything you want done should be inside the main method(method calls, etc)
    There are special circumstances but only instance variables should be outside of methods. Add the call to list to main.

  7. #7
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Everything you want done should be inside the main method(method calls, etc)
    There are special circumstances but only instance variables should be outside of methods. Add the call to list to main.
    i'm sorry i dont understand what you mean...i am very new to java i don't know why this was assigned to such a novice group but do you think you could elaborate a little more? thanks in advance

  8. #8
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    ok i understand what you meant now but the output is giving me
    1
    2
    3
    4
    5
    6
    7
    8
    9

    why is this so?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Haha, I can tell you are a novice, but that's fine, I hope you understand what I meant. Your loop currently is using println, which puts the string data on console and then adds a new line character. You will need to use System.out.println and System.out.print. Try to figure out which one goes where.

    Although you can't tell, this is actually a very good exercise for a novice. It demonstrated the use of loops, nested loops, arrays, and the difference between print and println.
    Last edited by sunde887; 05-04-2011 at 02:40 AM.

  10. #10
    ranjiths's Avatar
    ranjiths is offline Member
    Join Date
    Sep 2009
    Location
    India
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by DMarsh12 View Post
    ok i understand what you meant now but the output is giving me
    1
    2
    3
    4
    5
    6
    7
    8
    9

    why is this so?
    Modify your code snippet to the following.


    for (int R=0; R<3;R++)
    {
    for (int C=0; C<3;C++)
    System.out.print(x[R][C] + " ");
    System.out.println();
    }

    The additional System.out.println command is to take the cursor position to the next line after printing every three numbers.

    Any more help needed?

    - Ranjith
    All that a fellow passenger on this earth expects from you is your loving words and support. Never talk in a way that hurts others or insults them. Sarvejana Sukhinobavanthu.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Ranjith,

    Use Code Tags while posting your code hence forth.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by goldest View Post
    Ranjith,

    Use Code Tags while posting your code hence forth.

    Goldest
    Better still, don't post it. Ranjith has already been reported for several poor responses and oversize signature.

    db

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Darryl.Burke View Post
    Better still, don't post it. Ranjith has already been reported for several poor responses and oversize signature.

    db
    OOPS! Thanks for the info though. :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  14. #14
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    thank you all very much..it is working fine now i appreciate it

    EDIT

    i thought id be able to do the second part of the project with the information i have but its not working to his standards...

    i am supposed to be able to print out

    x x x
    x 5 x
    x x x where the xs represent blank spaces as well as

    x x 3
    x 5 x
    7 x x

    1 x x
    x 5 x
    x x 9

    1 x 3
    x x x
    7 x 9

    please any help is appreciated.
    Last edited by DMarsh12; 05-04-2011 at 04:40 PM.

  15. #15
    ranjiths's Avatar
    ranjiths is offline Member
    Join Date
    Sep 2009
    Location
    India
    Posts
    19
    Rep Power
    0

    Thumbs down

    Quote Originally Posted by Darryl.Burke View Post
    Better still, don't post it. Ranjith has already been reported for several poor responses and oversize signature.

    db
    It is only you who has reported. Rather than giving such responses, try solving the question asked. My response to this message of yours at Hi I am Ranjith Reintroducing myself on this day
    All that a fellow passenger on this earth expects from you is your loving words and support. Never talk in a way that hurts others or insults them. Sarvejana Sukhinobavanthu.

  16. #16
    ranjiths's Avatar
    ranjiths is offline Member
    Join Date
    Sep 2009
    Location
    India
    Posts
    19
    Rep Power
    0

    Smile

    Quote Originally Posted by DMarsh12 View Post
    thank you all very much..it is working fine now i appreciate it

    EDIT

    i thought id be able to do the second part of the project with the information i have but its not working to his standards...

    i am supposed to be able to print out

    x x x
    x 5 x
    x x x where the xs represent blank spaces as well as

    x x 3
    x 5 x
    7 x x

    1 x x
    x 5 x
    x x 9

    1 x 3
    x x x
    7 x 9

    please any help is appreciated.
    The following will do the work:

    Java Code:
    int numbers[][]= {{1,2,3},{4,5,6},{7,8,9}};
    for(int i=1; i<=4;i++)
    {
    for(int j=0;j<=2;j++)
    {
    for(int k=0;k<=2;k++)
    {
    if  (   (i==1&&j=1&&k==1)  ||   (i==2&&((j==0&&k==2)||(j==1&&k==1)||(j==2&&k==0)))     ||=     (i==3&&((j==0&&k==0)||(j==1&&k==1)||(j==2&&k==2)))    ||(i==4&&((j==0&&k==0)||(j==0&&k==2)||(j==2&&k==0)||(j==2&&k==2))))
    System.out.println(numbers[j][k]);
    else
    System.out.println(" ");
    
    }
    }
    }
    Alternately you can split the four patterns into four programs and write the code for each separately.
    Here is the algorithm for each:
    For each of the four pattern a nested for iteration is used with variables i and j to refer to the array element. For each iteratiuon the number is printed if the following conditions are satisfied
    Pattern 1: i=1 j=1
    Pattern 2: i+j=2 and i<=j
    Pattern 3: i+j=2 and i>=j
    Pattern 4: i+j=2 and i!=j

    Here is the code

    Pattern 1:
    Java Code:
    int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
    for(int i=0;i<=2;i++)
    {
    for(int j=0;j<=2;j++)
    {
    if(i==1&&j==1)
    System.out.println(a[i][j]);
    else
    System.out.println(" ");
    }
    }
    Pattern 2:

    Java Code:
    int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
    for(int i=0;i<=2;i++)
    {
    for(int j=0;j<=2;j++)
    {
    if( i+j==2  && i<=j)
    System.out.println(a[i][j]);
    else
    System.out.println(" ");
    }
    }
    Pattern 3:
    Java Code:
    int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
    for(int i=0;i<=2;i++)
    {
    for(int j=0;j<=2;j++)
    {
    if( i+j==2  && i>=j)
    System.out.println(a[i][j]);
    else
    System.out.println(" ");
    }
    }
    Pattern 4:
    Java Code:
    int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
    for(int i=0;i<=2;i++)
    {
    for(int j=0;j<=2;j++)
    {
     if(i+j==2  && i!=j)
    System.out.println(a[i][j]);
    else
    System.out.println(" ");
    }
    }
    Hope that your question is solved.
    If you have any other doubts please reply.

    - Ranjith
    All that a fellow passenger on this earth expects from you is your loving words and support. Never talk in a way that hurts others or insults them. Sarvejana Sukhinobavanthu.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 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
  •