|
-
April 3rd, 2011, 01:48 PM
#1
non-static method create(java.lang.String) cannot be referenced from a static context
I have created a TicTacToe class that has a 2 dimension array as it instanced variable.
It has a create() factory method that takes a string or X and O's, creates a nxn array out of the string, then returns a TicTacToe object made with the array.
I am getting the following error
java:12 non-static method create(java.lang.String) cannot be referenced from a static context
test=TicTacToe.create("X,X, ,O,X,O, ,O,X");
I don't know what id going on because I have modeled this after a similar program that functioned perfectly.
Code:
public class A5Q3{
public static void main (String[]args){
processGames();
}
public static void processGames()
{
TicTacToe test;
test=TicTacToe.create("X,X, ,O,X,O, ,O,X");
System.out.println(test.checkWinner());
}
}
class TicTacToe{
String[][] array;
public TicTacToe( String[][] array )
{
this.array=array;
}
public TicTacToe create(String input)
{
TicTacToe newTic;
String[] temp=input.split(",");
String[][]array;
int size=0;
int position=0;
size=Math.sqrt(temp.size());
array=new String[size][size];
for(int i=0; i<size;i++)
{
for(int j=0;j<size;j++)
{
array[i][j]=temp[position];
position++;
}
}
newTic=new TicTacToe(array);
return newtic;
}
//this method checks if there is a row with all the same values and returns a boolean
public String checkWinner()
{
boolean winner;
String winningChar;
String firstChar;
//check rows
for(int i=0;i<array.length&&winner==false;i++)
{
firstChar=array[i][0];
for(int j=0;j<array.length;j++)
{
winner=true;
if(firstChar!=array[i][j])
{
winner=false;
}
}
if(winner)
{
winningChar=firstChar;
}
}
//check columns
for(int i=0;i<array.length&&winner==false;i++)
{
firstChar=array[0][i];
for(int j=0;j<array.length;j++)
{
winner=true;
if(firstChar!=array[j][i])
{
winner=false;
}
}
if(winner)
{
winningChar=firstChar;
}
}
//check front diagnal
for(int i=1;i<array.length&&winner==false;i++)
{
winner=true;
firstChar=array[0][0];
if(firstChar!=array[i][i])
{
winner=false;
}
if(winner&&i==array.length-1)
{
winningChar=firstChar;
}
}
//check back diagnal
for(int i=0;i<array.length&&winner==false;i++)
{
winner=true;
if(array[0][array.length-1]!=array[array.length-1-i][i])
{
winner=false;
}
if(winner&&i==array.length-1)
{
winningChar=firstChar;
}
}
}
public void print()
{
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array.length;j++)
{
System.out.println(array[i][j]+" ");
}
}
}
}
-
April 3rd, 2011, 07:48 PM
#2
Re: non-static method create(java.lang.String) cannot be referenced from a static con
You need to add the static keyword to the create method in the TicTacToe class if you want to invole it without creating an instance of TicTacToe. So change:
public TicTacToe create(String input)
to
public static TicTacToe create(String input)
There are several other problems that I will leave to you to ponder.
Ray
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|