Results 1 to 11 of 11
- 03-17-2010, 09:04 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
String toCharArray problem - static context
Hello people, im trying to load the characters from a String word into an array, however i am getting an error while trying to use the String field from another class to the class which would load this words characters in an array.
Here is my code for the String value represented by the field "targetWord" which i need to use in my other class.
public class Database
{
public String targetWord;
//constructor ommited
/**
*
*/
public String getWord() {
int n = generator.nextInt(anArrayOfTargetWords.length);
targetWord = anArrayOfTargetWords[n];
return targetWord;
}
}
Here is my other class where the array of characters is made from the "targetWord":
public class Word
{
// Storage for letters.
public char letters[] = Database.targetWord.toCharArray();
The error is "non-static variable targetWord cannot be refferenced from a static context" yet i am unsure to how i am referencing in a static context.
Othere than that i believe the code is correct in loading the characters from the "targetWord" into an array.
I need help as soon as possible as this is driving me crazy, thanks.
- 03-17-2010, 09:13 AM #2
Go thru the java basics in sun site and then do experiments.
targetWord is a non static variable .U have to create instance for Database class and call.Ramya:cool:
- 03-17-2010, 09:29 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
This
is how you are "referencing in a static context".Java Code:Database.targetWord.toCharArray();
- 03-17-2010, 09:53 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Thanks guys i made a silly mistake, i forgot to include the database object in my code which has now fixed it :D, moving on i need to now assign each characted held in the array with a boolean value...any ideas or starting points?
This is to indicate wheter or not in my project the character has been guessed by the user
- 03-17-2010, 10:00 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Uhm, create a boolean array of the same size as the character array?
- 03-17-2010, 10:00 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Or create a class that holds both the char value and a boolean value and create an array of those to hold both?
- 03-17-2010, 11:19 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Thanks for the reply, i not sure how the code for this would look like do you mean something like:
public class Word
{
// Storage for letters.
public char letters[];
public boolean letters1[];
/**
*
*/
public void toArray()
{
letters = Game.myDatabase.targetWord.toCharArray();
letters1 = letters;
But this does not work because char and boolean cannot be in the same array, but do you mean somehow incorporating the char array into the boolean one?
The exact question im trying to address is:
"A class is needed to hold a number of Letter objects in an ArrayList or array (whichever you prefer). The Letter objects need to store individual characters from the current target word with a boolean value which indicates whether the player has guessed this letter yet."
I now think that i am going along the wrong lines with my previous array, is my code correct so far without the boolean part?
- 03-17-2010, 11:22 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
No one said anything about trying to assign one to the other.
Then see the second option posted (which that requirement is telling you to do) and create a class Letter using both a char and a boolean and create an array of those.
- 03-17-2010, 12:29 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Oh ok thanks alot for clearing that up:), but still im unsure on how to create an array of both char and boolean value:confused:, could you please provide a code sample of how to go about this?
- 03-19-2010, 12:19 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Can anybody please help me, why not give me a sample code to work on please, im running out of time for this and have tried everything without any luck.
- 03-19-2010, 01:28 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Similar Threads
-
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks