Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Help needed with array

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed with array

    Hey I'm new to this site I joined because I have a problem with my code I don't know how to make a array global I have a main and two classes that all need to access the same array to read and fill in information into it I've tried to place the array in different places but it still doesn't become global.
    If you need me to post the code I can but its just a simple Tic Tac Toe game so don't think you will need it to help
    Thanks in advance Pacina


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help needed with array

    how to make a array global
    What do you mean by "global"? Do you mean a class member variable? Define/declare it outside of any method.

    For methods in other classes to access the array, there will need to be a way to pass a reference to the array to the methods that want to access it. That is normally done with a getter method or it could be passed in a constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    Yeah I want to be able to access it from the main and the two classes do you have an example of the getter method never heard of that and the constructor as I haven't done anything with that yet.
    Is there not a easy way like in c++ where you just declare outside of your main and that makes it global and can be access from everywhere in the program.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help needed with array

    Defining Globals is not a recommended technique for OOP.

    A getter is a method that returns a value.

    When you define a class using a new statement, you can pass args to the constructor.
    = new TheClass(theargs go here);
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    Sound I think I know what to do I'll try that now and see how it goes
    Thanks
    Pacina

    --- Update ---

    Still don't get I though I understood what you are saying but I don't so I'll post the code
    main
     
    public class Main 
    {
     
    	public static void main(String[] args) 
    	{
    		int [] [] grid = {{1,2,3},
    		  		          {4,5,6},
    		  		          {7,8,9}};
     
    		System.out.println("\tWelcome to my game of Tic Tac Toe");
    		System.out.println("\t     Please use Cap Lock's");
    		System.out.println("\t     Player 1 uses X");
    		System.out.println("\t     Player 2 uses O");
     
    		do
    		{
    			player1choise player1choiseObject = new player1choise();
    			player1choiseObject.choise1();
     
    			player2choise player2choiseObject = new player2choise();
    			player2choiseObject.choise2();
     
     
    		}while(grid[0][0] != 'X' || grid[0][0] != 'O' && grid[0][1] != 'X' || grid[0][1] != 'O' && grid[0][2] != 'X' || grid[0][2] != 'O' && grid[1][0] != 'X' || grid[1][0] != 'O' 
    		    && grid[1][1] != 'X' || grid[1][1] != 'O' && grid[1][2] != 'X' || grid[1][2] != 'O' && grid[2][0] != 'X' || grid[2][0] != 'O' && grid[2][1] != 'X' || grid[2][1] != 'O'
    			&& grid[2][2] != 'X' || grid[2][2] != 'O');
     
    	}
    }
    class 1
    import java.util.Scanner;
    public class player1choise 
    {
    	public void choise1(int grid[][])
    	{
    		Scanner myinput = new Scanner(System.in);
    		char player1choise;
    		String player1name;
     
    		System.out.println("\n\nPlease enter player 1 name ");
    		player1name = myinput.nextLine();
     
    		do
    		{
    			System.out.println(player1name+ " Enter where you would like to place your peice");
    			player1choise = myinput.findInLine(".").charAt(0);
    		}while(player1choise >=1 || player1choise <=9);
     
    		if(player1choise == 1){
    			grid [0][0] = 'X';
    		}else if (player1choise == 2){
    			grid [0][1]  = 'X';
    		}else if (player1choise == 3){
    			grid [0][2] = 'X';
    		}else if (player1choise == 4){
    			grid [1][0] = 'X';
    		}else if (player1choise == 5){
    			grid [1][1] = 'X';
    		}else if (player1choise == 6){
    			grid [1][2] = 'X';
    		}else if (player1choise == 7){
    			grid [2][0] = 'X';
    		}else if (player1choise == 8){
    			grid [2][1] = 'X';
    		}else if (player1choise == 9){
    			grid [2][2] = 'X';
    		}
    	}
     
    }
    class 2
    import java.util.Scanner;
    public class player2choise 
    {
    	public void choise2(int grid [][])
    	{
    		Scanner myinput = new Scanner(System.in);
    		char player2choise;
    		String player2name;
     
    		System.out.println("\nPlease enter player 2 name ");
    		player2name = myinput.nextLine();
     
    		do
    		{
    		System.out.println(player2name+ " Enter where you would like to place your peice");
    		player2choise = myinput.findInLine(".").charAt(0);
    		}while(player2choise >=1 || player2choise <=9);
     
    		if(player2choise == 1){
    			grid [0][0] = 'O';
    		}else if (player2choise == 2){
    			grid [0][1]  = 'O';
    		}else if (player2choise == 3){
    			grid [0][2] = 'O';
    		}else if (player2choise == 4){
    			grid [1][0] = 'O';
    		}else if (player2choise == 5){
    			grid [1][1] = 'O';
    		}else if (player2choise == 6){
    			grid [1][2] = 'O';
    		}else if (player2choise == 7){
    			grid [2][0] = 'O';
    		}else if (player2choise == 8){
    			grid [2][1] = 'O';
    		}else if (player2choise == 9){
    			grid [2][2] = 'O';
    		}
    	}
     
    }

    Hopefully you will be able to tell me which part I need to change

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help needed with array

    What array is supposed to be accessed by what methods in what classes?

    What happens when the posted code is compiled?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    There's only one array and it needs to be accessed from everything its a tic tac toe game and each class is the player placing their X or O on the board one class for player one and one class for player two the main just calls each class pretty much that's why I want the array to be global so it will be updated after each turn and when the board is full the do while loop in main will be true and then I will have the code cheek which player won if any.
    The program doesn't have to be perfect I'm just doing it so I get used to using classes and calling them and brushing up on arrays as I didn't do great in my last exam with arrays

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help needed with array

    What happens when the posted code is compiled?

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    now I just get errors but before I changed some things it ran through the first class then gave errors I'll get it back to that and post exact errors thanks for your time and advice talk tomorrow

  10. #10
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Help needed with array

    Quote Originally Posted by Pacina View Post
    Hey I'm new to this site I joined because I have a problem with my code I don't know how to make a array global I have a main and two classes that all need to access the same array to read and fill in information into it I've tried to place the array in different places but it still doesn't become global.
    If you need me to post the code I can but its just a simple Tic Tac Toe game so don't think you will need it to help
    Thanks in advance Pacina
    I think you need this

    ...removed
    Last edited by copeg; March 19th, 2013 at 09:31 AM. Reason: Removed spoonfed code
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    Quote Originally Posted by Tamilarasi View Post
    I think you need this

    ...removed
    Thanks will try that when I get home tonite and post back

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help needed with array

    @Tamilarasi, please read the forum rules and the following:
    http://www.javaprogrammingforums.com...n-feeding.html
    You have received an infraction as a result. I encourage you to read the message accompanying that infraction carefully.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    @copeg why is Tamilarasi getting an infraction for trying to help a member I asked how to do something that is not meant to be done in OOP so there wont be a tut on how to do it and Tamilarasi just gave me a snippet of code that I could try to get the program running to find any other bugs that are in it while I try and figure out the correct way to access an array from each class.
    @Tamilarasi Sorry about this thanks again for trying to help.

  14. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help needed with array

    Quote Originally Posted by Pacina View Post
    @copeg why is Tamilarasi getting an infraction for trying to help a member I asked how to do something that is not meant to be done in OOP so there wont be a tut on how to do it and Tamilarasi just gave me a snippet of code that I could try to get the program running to find any other bugs that are in it while I try and figure out the correct way to access an array from each class
    Because all Tamilarasi has done is give you code - no explanation about what it does, how it may solve your problem, or anything else. This is not what these forums, or programming in general, is about. One of the hardest skills to learn when it comes to programming is problem solving, and these forums are about helping you solve them, not solving the problem for you. The link I posted above clearly explains this and other reasons why it is discouraged, and the forum rules lay out that spoonfed posts will be edited. Lastly this is not the first time Tamilarasi has done this despite being warned otherwise (this includes other prgramming forums as well).

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Help needed with array

    Quote Originally Posted by copeg View Post
    Because all Tamilarasi has done is give you code - no explanation about what it does, how it may solve your problem, or anything else. This is not what these forums, or programming in general, is about. One of the hardest skills to learn when it comes to programming is problem solving, and these forums are about helping you solve them, not solving the problem for you. The link I posted above clearly explains this and other reasons why it is discouraged, and the forum rules lay out that spoonfed posts will be edited. Lastly this is not the first time Tamilarasi has done this despite being warned otherwise (this includes other prgramming forums as well).
    First of all i posted only the basic issue for how to pass the array from one to another class. Then i explained what is issue how to fix that issue in within comments for posted code.once you read complete code before delete that.And the code has some issues. But i posted only basic code. Finally how to say that [qoute]Lastly this is not the first time Tamilarasi has done this despite being warned otherwise (this includes other prgramming forums as well).[/qoute]. if you read my all posts for all forums.

    --- Update ---

    Quote Originally Posted by Pacina View Post
    Hey I'm new to this site I joined because I have a problem with my code I don't know how to make a array global I have a main and two classes that all need to access the same array to read and fill in information into it I've tried to place the array in different places but it still doesn't become global.
    If you need me to post the code I can but its just a simple Tic Tac Toe game so don't think you will need it to help
    Thanks in advance Pacina
    First you concern-rate how to pass the array from one class to another.
    you search regarding this you will lot of answers
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  16. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with array

    Have my code updated took a lot of things out to get the main program working will add them back in again when the game is working perfect the only problem I have now eith the code I have is my do while loop is not going true when the array is full so it doesn't exit it here's the full program
    Main:
     
    public class Main 
    {
    	static char [] [] grid = {{'A','B','C'},
    	                          {'D','E','F'},
    	                          {'G','H','I'}};
    	public static void main(String[] args) 
    	{
     
     
    		System.out.println("\tWelcome to my game of Tic Tac Toe");
    		System.out.println("\t     Player 1 uses X");
    		System.out.println("\t     Player 2 uses O");
     
    		for (int i=0;i<grid.length;i++)
    		{
    			for (int j=0;j<grid.length;j++)
    			{
    				System.out.print(grid[i][j]+" ");
    			}
    			System.out.println();
    		}
     
    		do
    		{
    			System.out.println();
    			player1choise player1choiseObject = new player1choise();
    			player1choiseObject.choise1(grid);
     
    			player2choise player2choiseObject = new player2choise();
    			player2choiseObject.choise2(grid);
     
    		}while(grid[0][0] != 'X' || grid[0][0] != 'O' && grid[0][1] != 'X' || grid[0][1] != 'O' && grid[0][2] != 'X' || grid[0][2] != 'O' && grid[1][0] != 'X' || grid[1][0] != 'O' 
    		    && grid[1][1] != 'X' || grid[1][1] != 'O' && grid[1][2] != 'X' || grid[1][2] != 'O' && grid[2][0] != 'X' || grid[2][0] != 'O' && grid[2][1] != 'X' || grid[2][1] != 'O'
    			&& grid[2][2] != 'X' || grid[2][2] != 'O');
     
    	}
    }
    Class 1
    import java.util.Scanner;
    public class player1choise 
    {
    	public void choise1(char [][] grid)
    	{
     
    		Scanner myinput = new Scanner(System.in);
    		char player1choise;
     
    		System.out.println("Player 1 enter where you would like to place your peice");
    		player1choise = myinput.findInLine(".").charAt(0);
     
    		if(player1choise == 'A'){
    			grid [0][0] = 'X';
    		}else if (player1choise == 'B'){
    			grid [0][1] = 'X';
    		}else if (player1choise == 'C'){
    			grid [0][2] = 'X';
    		}else if (player1choise == 'D'){
    			grid [1][0] = 'X';
    		}else if (player1choise == 'E'){
    			grid [1][1] = 'X';
    		}else if (player1choise == 'F'){
    			grid [1][2] = 'X';
    		}else if (player1choise == 'G'){
    			grid [2][0] = 'X';
    		}else if (player1choise == 'H'){
    			grid [2][1] = 'X';
    		}else if (player1choise == 'I'){
    			grid [2][2] = 'X';
    		}
     
    		for (int i=0;i<grid.length;i++)
    		{
    			for (int j=0;j<grid.length;j++)
    			{
    				System.out.print(grid[i][j]+" ");
    			}
    			System.out.println();
    		}
    	}
     
    }
    Class 2
    import java.util.Scanner;
    public class player2choise 
    {
    	public void choise2(char [][] grid)
    	{
     
    		Scanner myinput = new Scanner(System.in);
    		int player2choise;
     
     
    		System.out.println("Player 2 enter where you would like to place your peice");
    		player2choise = myinput.findInLine(".").charAt(0);
     
    		if(player2choise == 'A'){
    			grid [0][0] = 'O';
    		}else if (player2choise == 'B'){
    			grid [0][1] = 'O';
    		}else if (player2choise == 'C'){
    			grid [0][2] = 'O';
    		}else if (player2choise == 'D'){
    			grid [1][0] = 'O';
    		}else if (player2choise == 'E'){
    			grid [1][1] = 'O';
    		}else if (player2choise == 'F'){
    			grid [1][2] = 'O';
    		}else if (player2choise == 'G'){
    			grid [2][0] = 'O';
    		}else if (player2choise == 'H'){
    			grid [2][1] = 'O';
    		}else if (player2choise == 'I'){
    			grid [2][2] = 'O';
    		}
     
    		for (int i=0;i<grid.length;i++)
    		{
    			for (int j=0;j<grid.length;j++)
    			{
    				System.out.print(grid[i][j]+" ");
    			}
    			System.out.println();
    		}
    	}
     
    }

    This is the part I'm having trouble with now
    do
    		{
    			System.out.println();
    			player1choise player1choiseObject = new player1choise();
    			player1choiseObject.choise1(grid);
     
    			player2choise player2choiseObject = new player2choise();
    			player2choiseObject.choise2(grid);
     
    		}while(grid[0][0] != 'X' || grid[0][0] != 'O' && grid[0][1] != 'X' || grid[0][1] != 'O' && grid[0][2] != 'X' || grid[0][2] != 'O' && grid[1][0] != 'X' || grid[1][0] != 'O' 
    		    && grid[1][1] != 'X' || grid[1][1] != 'O' && grid[1][2] != 'X' || grid[1][2] != 'O' && grid[2][0] != 'X' || grid[2][0] != 'O' && grid[2][1] != 'X' || grid[2][1] != 'O'
    			&& grid[2][2] != 'X' || grid[2][2] != 'O');

    What do I do to update the array so the while condition is true and it quits????????????????????

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help needed with array

    update the array so the while condition is true and it quits
    The while loop continues while the condition is true. Are you asking how to make that ugly, long condition false, so the loop will exit?

    The problem with using an OR operator to connect all the sub conditions is that if ONE of them is true, that makes the whole condition true.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Array help needed
    By The Lost Plot in forum Collections and Generics
    Replies: 5
    Last Post: March 12th, 2010, 12:22 PM
  2. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 08:49 PM
  3. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  4. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 05:49 AM
  5. Array program help needed
    By SCM in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2009, 11:28 PM