Don't create multiple threads for the same problem if you don't intend on reading the replies given in your previous threads. In this thread, you were given a thorough explanation of how arrays work; if you still don't get it then maybe this assignment is too tough for you.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
don't use two arrays, just use one, for both initializing and printing.
also, in your
public void arr3(){
...
}
method, you are referring to Array arr, which is not declared within the method, and, I think after looking at your other methods, where it's passed as an argument, it's not declared in the class either (but is possible off course)
could you please show all your code and tell us what it is you do try to accomplish?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
Unless you post a *complete* working code, there is no way we can *see* what's wrong on your code. If not that, then present a SSCE which explains your situation. Either that or use an IDE like Eclipse to help you in debugging your application.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
You try to display something which you don't even process after declaring i.e. the local array `arr' declared in main(); hence the given result. Either pass the local variable `arr' to your class instances or write a display method which works on class members i.e. `arr' of Class1.
But come to think of it; this is possibly the one of the most ill designed class hierarchy I have ever seen; the naming conventions not being followed is yet another woe. Consider reading on some good material as mentioned in the Java forum sticky.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Post the formal problem definition as mentioned in your assignment and we might just help you with a pseudo code / class design.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Either pass the array reference declared in main() to your methods or just make sure that the display() method prints out the instance variable 'arr' instead of passing it an array declared in the main() method.
// Untested: Error handling omitted for brevity
class ArrayTest {
private String[][] twoDimArr;
public ArrayTest(int x, int y) {
// handle invalid input
twoDimArr = new String[x][y];
}
public void populateArray(InputStream in) {
// loop over the array, accept input from the source (here `in')
// and populate it.
}
public void displayArray() {
// loop over the array and display each element.
}
public static void main(final String[] args) {
ArrayTest t = new ArrayTest(3, 3);
t.populateArray(System.in);
t.display();
}
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
> The above post is all in one class and not in different classes like I had wanted.
Of course, otherwise I would have ended up giving you the entire solution.
public void in(InputStream in)
{
class2 class3Obj = new class3();
x=((class3)class3Obj).get_x();
y=((class3)class3Obj).get_y();
for(int i=0;i<arr.length;i++)
{
String[] tmp = arr[i];
for (int j=0;j<tmp.length;j++)
{
tmp[j]=" X ";
arr[x][y]=" O ";
}
}
}
Again a lot of problems with your code.
- Don't create a `public' Scanner instance; use the `in' passed in the `in' method of class1 to create a Scanner , that's the entire point of passing in an InputStream .
- If you need to set something at the position x,y of your array, do it once, not in a loop. It is because of this loop that the previous values are getting wiped off. Also check the values of `x' and `y' to avoid ArrayIndexOutOfBoundsException .
Any reason why `class3' extends `class2' and `class3' has a reference to `class1'? Your design has a lot of coupling and almost no cohesion. Like I mentioned previously, reconsider your design choices, discuss with your peers and professors about this assignment and read some good tutorials/books.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
If you don't ever change x or y, it's going to keep overwriting the same element forever.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
So what you really want is something similar to this perhaps? Assuming you have a 2D array "attributeArray" of "Attribute" objects that contain your data
private Attribute getAttribute() {
int x = <em>promptForX()</em>;
int y = <em>promptForY()</em>;
return attributeArray[x][y];
}
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Then when the user inputs (1,2) later for display not input, it displays the details in (1,2).
That was the point of the skeletal code above. The details are the "Attribute" object. You can call it whatever you want. All you are doing is storing and retrieving a single object at a position in a 2D array.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847