Results 1 to 11 of 11
  1. #1
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

    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:

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    This
    Java Code:
    Database.targetWord.toCharArray();
    is how you are "referencing in a static context".

  4. #4
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    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

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm, create a boolean array of the same size as the character array?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Or create a class that holds both the char value and a boolean value and create an array of those to hold both?

  7. #7
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    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?

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  9. #9
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    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?

  10. #10
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Unhappy

    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.

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Could we see your attempt at making and using that Letter class?

Similar Threads

  1. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •