Results 1 to 16 of 16
Thread: 2-D array help
- 05-04-2011, 01:41 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
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
- 05-04-2011, 02:01 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
toJava Code:public class list(int[]x)
Other than that the code seems fine.Java Code:public static void list(int[]x)
- 05-04-2011, 02:08 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
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:
- 05-04-2011, 02:10 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are declaring the method inside main, use a closing curly brace to end main before writing list.
- 05-04-2011, 02:16 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
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
- 05-04-2011, 02:19 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-04-2011, 02:23 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
- 05-04-2011, 02:28 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
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?
- 05-04-2011, 02:36 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-04-2011, 07:41 AM #10
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?
- RanjithAll 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.
- 05-04-2011, 07:44 AM #11
Ranjith,
Use Code Tags while posting your code hence forth.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-04-2011, 08:13 AM #12
- 05-04-2011, 10:42 AM #13
- 05-04-2011, 04:29 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
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.
- 05-05-2011, 10:22 AM #15
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.
- 05-05-2011, 10:46 AM #16
The following will do the work:
Alternately you can split the four patterns into four programs and write the code for each separately.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(" "); } } }
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:
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==1&&j==1) 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.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(" "); } }
If you have any other doubts please reply.
- RanjithAll 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
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks