Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java - Problem instantiating 2D byte array
New Posts  All Forums:Forum Nav:

Java - Problem instantiating 2D byte array

post #1 of 5
Thread Starter 
Good evening,
I'm unsure why but I'm having trouble instantiating my 2D byte array. Upon calling the default constructor, and attempting to print a field from the 'gb' object using default constructor. I'm not sure why this is confusing me so much, but it's been a long week. 2nd all-nighter in a row.

Goal of program: simulate 3x3 matrix of a Tic-Tac-Toe board
Code:
package fisherty.hw4;

public class TicTacToe {
   //Declare instance variables
    public byte[][] GameBoard;//declare GameBoard as 3x3 byte matrix 
    
/***************************************************************************************************
 * Main Program
 **************************************************************************************************/
     public static void main(String args[]){
        TicTacToe gb = new TicTacToe();
        System.out.println(gb[3][2]);
    }
/***************************************************************************************************
 * Constructors
 **************************************************************************************************/
    public TicTacToe(){                                       
        GameBoard = new byte[3][3];     
    }
//**************************************************************************************************
}//TicTacToe class

Thank you for your time,
Tyler J. Fisher
Edited by TylerJFisher - 11/18/11 at 4:27pm
Workstation #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 940 BIOSTAR TA790GX 128M ATI Radeon HD 4770 OCZ Gold 4GB (2x2GB) DDR2 (PC2 6400) 
Hard DriveOptical DriveOSMonitor
3x Seagate Barracuda 7200.12 500GB (RAID 0) 3x Samsung DVD+RW & 1x LG DVD+RW Windows 7x64 Ultimate (CentOS x64 [Virtual]) HP vs17 
PowerCase
OCZ ModXStream Pro 700W Antec Nine-Hundred 
  hide details  
Reply
Workstation #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 940 BIOSTAR TA790GX 128M ATI Radeon HD 4770 OCZ Gold 4GB (2x2GB) DDR2 (PC2 6400) 
Hard DriveOptical DriveOSMonitor
3x Seagate Barracuda 7200.12 500GB (RAID 0) 3x Samsung DVD+RW & 1x LG DVD+RW Windows 7x64 Ultimate (CentOS x64 [Virtual]) HP vs17 
PowerCase
OCZ ModXStream Pro 700W Antec Nine-Hundred 
  hide details  
Reply
post #2 of 5
What error is it giving you?
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
1100t Asus m4A78T-E HIS 5850 4G 
Hard DriveOS
60 SS, + 3T Windows 7 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
1100t Asus m4A78T-E HIS 5850 4G 
Hard DriveOS
60 SS, + 3T Windows 7 
  hide details  
Reply
post #3 of 5
Quote:
Originally Posted by TylerJFisher View Post

Good evening,
I'm unsure why but I'm having trouble instantiating my 2D byte array. Upon calling the default constructor, and attempting to print a field from the 'gb' object using default constructor. I'm not sure why this is confusing me so much, but it's been a long week. 2nd all-nighter in a row.
Goal of program: simulate 3x3 matrix of a Tic-Tac-Toe board
Code:
package fisherty.hw4;
public class TicTacToe {
   //Declare instance variables
    public byte[][] GameBoard;//declare GameBoard as 3x3 byte matrix 
    
/***************************************************************************************************
 * Main Program
 **************************************************************************************************/
     public static void main(String args[]){
        TicTacToe gb = new TicTacToe();
        System.out.println(gb[3][2]);
    }
/***************************************************************************************************
 * Constructors
 **************************************************************************************************/
    public TicTacToe(){                                       
        GameBoard = new byte[3][3];     
    }

//**************************************************************************************************
}//TicTacToe class
Thank you for your time,
Tyler J. Fisher

This is the error I will probably get when I compile your code. (That is, if you have indeed provided the complete code).
Quote:
array required, but TicTacToe found
System.out.println(gb[2][2]);

Hmmm... I see that you are trying to "print" from the object by giving the index of an array.

As far as I know, you cannot do that since the object does not take a set of indices (as you added [3][2] with gb)

Did you actually mean to print the array called "Gameboard"? Well then you'll have to write:
Code:
System.out.println(Gameboard[3][2]);

By the way, even that will give you an "Arrayoutofbounds exception" because the array you created has only 3 rows and 3 columns with indices from 0 to 2 in both.

If I am correct in recognising your mistake and I have done it as you meant to do it, then you should know that even this will give an error since you cannot reference a non-static variable from a static context (i.e. you cannot reference Gameboard (a variable instantiated outside the main function) from inside the function).

For printing this index of the array, you'll have to use your object gb to access a function that prints this. Also, you can add the print statement inside the constructor.

Hope I understood your question correctly. Feel free to ask again if I have misunderstood.
Edited by {Unregistered} - 11/19/11 at 5:15am
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #4 of 5
Thread Starter 
Thanks for your responses! It told me that I was referring to my fishertyHW4.TicTacToe class instead of the Gameboard array. I ended up resolving the solution, and realized it was an out of bounds exception. Like I said, it was late. I was trying to call [3][2] but despite having instantiated 3x3 memory allocations, I forgot that arrays always start at 0. Thankfully, I slept last night.

Regards,
Tyler J. FIsher
Workstation #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 940 BIOSTAR TA790GX 128M ATI Radeon HD 4770 OCZ Gold 4GB (2x2GB) DDR2 (PC2 6400) 
Hard DriveOptical DriveOSMonitor
3x Seagate Barracuda 7200.12 500GB (RAID 0) 3x Samsung DVD+RW & 1x LG DVD+RW Windows 7x64 Ultimate (CentOS x64 [Virtual]) HP vs17 
PowerCase
OCZ ModXStream Pro 700W Antec Nine-Hundred 
  hide details  
Reply
Workstation #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 940 BIOSTAR TA790GX 128M ATI Radeon HD 4770 OCZ Gold 4GB (2x2GB) DDR2 (PC2 6400) 
Hard DriveOptical DriveOSMonitor
3x Seagate Barracuda 7200.12 500GB (RAID 0) 3x Samsung DVD+RW & 1x LG DVD+RW Windows 7x64 Ultimate (CentOS x64 [Virtual]) HP vs17 
PowerCase
OCZ ModXStream Pro 700W Antec Nine-Hundred 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by TylerJFisher View Post

Thanks for your responses! It told me that I was referring to my fishertyHW4.TicTacToe class instead of the Gameboard array. I ended up resolving the solution, and realized it was an out of bounds exception. Like I said, it was late. I was trying to call [3][2] but despite having instantiated 3x3 memory allocations, I forgot that arrays always start at 0. Thankfully, I slept last night.
Regards,
Tyler J. FIsher

Good to know you got it worked out.thumb.gif
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java - Problem instantiating 2D byte array