I have got two arrays. One for initializing and another for displaying. The display array always shows nothing if the initializing array has not been called. The problem now though is that if I want to insert something in a certain slot in the array nothing happens and I dont know why. Here is a sample code of my 3 arrays.

public void arr1(String[][] arr)
	{

		for(int i=0;i<arr.length;i++)
		{
			
			for (int j=0;j<coordinates[i].length;j++)
	
				arr[i][j]=" . ";
			//	arr[x][y]=" # "; //this seems to work but it is not what i want
		}
	}
	
	public void arr3()
	{			
		Class obj = new Class ();
		x=obj.getX();//gets input from a certain class
		y=obj.getY();
		for(int i=0;i<arr.length;i++)
		{	
			for (int j=0;j<arr[i].length;j++)
	
				arr[x][y]="#";//doesnt work
		}
	}
	
	public void arr3(String[][] arr)
	{

		for(int i=0;i<arr.length;i++)
		{

			for (int j=0;j<coordinates[i].length;j++)

				System.out.print(" "+arr[i][j]);
				System.out.println();
		}

	}

The above code does not compile because I will have to post the whole code here. I hope this is enough for me to get the help I need. Thanks

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.

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?

Comments
giving back the rep i wrongfully took away

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.

The post was clear but I have managed to figure it out anyway. Thanks

I figured out on how to make the arrays work but I cant seem to find out why the values that have been stored in an array in a specific method cannot be seen in another. Maybe I just didnt get it right but like in the initial post, when displayed, the values in the display array are still having the initialized value even if a different method that assigns a different value to certain points is called.

ps. I used a different function to display the fields because i want to display the values at a time when I call the function

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.

I have posted an edited version of the original code. It compiles and runs and is evident of what I am trying to do. Please Ignore any inconsistancies or anything concerning classes and help out on the arrays. Thank you

import java.util.Scanner;
public class Class1{

	protected int x;
	protected int y;
	protected String[][] arr=new String[10][10];
	public Scanner scan=new Scanner (System.in);

	public Class1 ()
	{

	}

	public void in(String[][] arr)
	{

		for(int i=0;i<arr.length;i++)
		{
			String[] tmp = arr[i];
			

			for (int j=0;j<tmp.length;j++)
				
			{	
				tmp[j]=" X ";

			}

		}


	}
	
	public void disp(String[][] arr)
	{
		int count_one=0;
		int count_two=0;
		System.out.print(" ");
		
		for(int i=0;i<arr.length;i++)
		{
			System.out.print("   "+count_one);
			count_one++;
		}
		
		System.out.println(" ");
		
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(" "+count_two);
			count_two++;
			
			 String [] tmp = arr[i];

			
			for (int j=0;j<tmp.length;j++)
			{
				System.out.print(" "+tmp[j]);
				
				}
				System.out.println();
		}
		
		System.out.println(" ");
	}
	
	public void add_arr(int x, int y)
	{			

		for(int i=0;i<arr.length;i++)
		{
		String[] tmp = arr[i];

				
			for (int j=0;j<tmp.length;j++)
			{
	
				arr[x][y]=" O ";
		
			}
		}
	}


}

class class2{
	
	protected int var_a;
	protected int var_b;
	public Scanner scan=new Scanner (System.in);
	
	public class2 ()
	{
		var_a=0;
		var_b=0;
	}
	
	public class2 (int var_a, int var_b)
	{
		this.var_a=var_a;
		this.var_b=var_b;
	}
	
	public int getvar_a()
	{
		System.out.print("Enter var_a: ");
		var_a=scan.nextInt();
		return var_a;
	}
	
	public int getvar_b()
	{
		System.out.print("Enter var_b: ");
		var_b=scan.nextInt();
		System.out.println();
		return var_b;
	}

	
}

class class3 extends class2{
	
	private String var_c;
	protected int x,y;
	private Class1 z;
	String[] tmp;
	public class3 ()
	{
		super ();
		var_c="n/a";
	}
	
	public class3 (String var_c,int var_a,int var_b)
	{
		super (var_a,var_b);
		this.var_c=var_c;
	}
	
	public String getvar_c()
	{
;
		return var_c;
	}
	
	public int get_x()
	{
		System.out.print("X-> ");
		x=scan.nextInt();
		
		System.out.println();
		
		return x;
	}
	
	public int get_y()
	{
	
		System.out.print("Y-> ");
		y=scan.nextInt();
		
		System.out.println();
		
		return y;
	}
	
	public void add_class3()
	{

		getvar_c();
		
		Class1 obj = new Class1 ();
		
		get_x();
		get_y();
		obj.add_arr(x,y);
	}
}


class main
{


	public static void main (String[] args)
	{
		int choice=0;
		int x, y;
		Scanner scan=new Scanner (System.in);
		String [][] arr=new String[10][10];

	
		class2 class3Obj = new class3();
		Class1 obj=new Class1();


		((class3)class3Obj).add_class3();

		obj.disp(arr);



	}
}

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.

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.

Like I had earlier mentioned this was just a draft code to show what I wanted to do and not the original one. Thank you for the suggestion on the array but I cant seem to figure out how to go about what you were talking about. If you could please explain with a simple example that would help alittle more. Thank you

Post the formal problem definition as mentioned in your assignment and we might just help you with a pseudo code / class design.

Well the whole assignment is about a level of a game but at the moment I would only appreciate help with the array part and try to figure out the rest.

First Class- I am trying to initialize an array to a certain value to be a default value in this class. I also want a display method here. The display method should be called at anytime and contain the values in each array field.

Second Class- I want the user to input certain details like coordinates i.e rows and columns and assign a certain character to these coordinates.

Third Class- This should be the main. which is menu driven. calls the method that asks for user input and also the method to display.

I hope this can help in a certain way.

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();
  }

}

The above post is all in one class and not in different classes like I had wanted. Anyway I have done something and the code puts the mark where I want it to be. However, on a second loop to check if the previous mark was stored in the coordinates, it looks like the coordinates are reseted again to the new x and y values. Here is the code.

import java.util.Scanner;
import java.io.*;
public class Class1{

	protected int x;
	protected int y;
	protected String[][] arr;
	public Scanner scan=new Scanner (System.in);


			
	public Class1 (int x, int y)
	{
		arr=new String[x][y];
	}
	
	
	public Class1 (String[][] arr)
	{
		this.arr=arr;
	}

	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 ";
			}

		}


	}
	
	public void disp()
	{
		int count_one=0,x=0,y=0;
		int count_two=0;
		System.out.print(" ");

		for(int i=0;i<arr.length;i++)
		{
			System.out.print("   "+count_one);
			count_one++;
		}
		
		System.out.println(" ");
		
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(" "+count_two);
			count_two++;
			
			 String [] tmp = arr[i];

			
			for (int j=0;j<tmp.length;j++)
			{
				System.out.print(" "+tmp[j]);
				
			}
				System.out.println();
		}
		
		System.out.println(" ");
	}
	
	public void add_arr(int x, int y)
	{			

		for(int i=0;i<arr.length;i++)
		{
		String[] tmp = arr[i];

				
			for (int j=0;j<tmp.length;j++)
				{
	
				arr[x][y]=" O ";
		
				}
		}
	}


}

class class2{
	
	protected int var_a;
	protected int var_b;
	public Scanner scan=new Scanner (System.in);
	
	public class2 ()
	{
		var_a=0;
		var_b=0;
	}
	
	public class2 (int var_a, int var_b)
	{
		this.var_a=var_a;
		this.var_b=var_b;
	}
	
	public int getvar_a()
	{
		System.out.print("Enter var_a: ");
		var_a=scan.nextInt();
		return var_a;
	}
	
	public int getvar_b()
	{
		System.out.print("Enter var_b: ");
		var_b=scan.nextInt();
		System.out.println();
		return var_b;
	}

	
}

class class3 extends class2{
	
	private String var_c;
	protected int x,y;
	private Class1 z;
	String[] tmp;
	public class3 ()
	{
		super ();
		var_c="n/a";
	}
	
	public class3 (String var_c,int var_a,int var_b)
	{
		super (var_a,var_b);
		this.var_c=var_c;
	}
	
	public String getvar_c()
	{
;
		return var_c;
	}
	
	public int get_x()
	{
		System.out.print("X-> ");
		x=scan.nextInt();
		
		System.out.println();
		
		return x;
	}
	
	public int get_y()
	{
	
		System.out.print("Y-> ");
		y=scan.nextInt();
		
		System.out.println();
		
		return y;
	}
	
}



class main
{

	public static void main (String[] args)
	{
		int a=4;
		while(a==4) //simple loop to keep prompting
		{
			int choice=0;
			int x, y;
		
			Class1 obj = new Class1(10,10);
    		obj.in(System.in);
    		obj.disp();
		}
	}
}

> 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.

I managed to figure out the array problem. Thanks alot. However, I now want to solve something different. I want a user to enter x and y values to get the details in those coordinates. I have created an array of objects that will be assigned to the 2D array but it seems to overwrite it. below is my sample code. Thanks for any help

//array declarations
	protected Object[][] 2D_arr=new Object[10][10];
	protected Class2[] my_arr=new Class2[4];
//code to store values
		for (int i=0;i<my_arr.length;i++)
		{
			my_arr[i]=new Class2(" A ","B",2,2);
			2D_arr[y][x]=my_arr;
		}

//code to display
			System.out.println("Output");
			System.out.println("First "+my_arr[1]);
			System.out.println("Second "+my_arr[2]);
			System.out.println("Third "+my_arr[3]);

I hope this is helpful enough. I have changed the values because I cannot post my original code here.

That is how I get the problem. X and Y are input by the user in one method with attributes. What I want to do is to be able to prompt the user from another method for coordinates x,y and then display the attributes that were input earlier. My thought came up to the above code which overwrites. How can I overcome this?

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 = [I]promptForX()[/I];
  int y = [I]promptForY()[/I];
  return attributeArray[x][y];
}

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 = [I]promptForX()[/I];
  int y = [I]promptForY()[/I];
  return attributeArray[x][y];
}

I dont think the above does the trick. It looks like it gets the X and Y points and also their attributes maybe. What I want to do is after you get the X and Y points say (1,2), get user input and store the details in this point (1,2). Then when the user inputs (1,2) later for display not input, it displays the details in (1,2).

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.

Am sorry but am not getting this at all. I understand what you are saying and that is what I had in mind. But the details I want to store are in another method. So I dont know how to go about it. I cant even seem to figure out how to call it in the other method because if I do something like "array[x][y].attribute" I still dont get the result. I also created a new object to try out differently but to no avail.This is what I tried to do

public class2 getPoint()
	{
            class2 obj=new class2("A",/*user input*/,2,3,2);
 	     x=getX();
	     y=getY();
            return array[x][y];
         }

	public void disp()
	{
	    System.out.println("Result");
            System.out.println("Attr1 "+array[y][x]);
        }
This question has already been answered. Start a new discussion instead.