Results 1 to 5 of 5
  1. #1
    Medieve is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Having problems with static booleans

    I've been going through all the forums I can find, all the books I can get my hands on, all the people that I know personally, and I can't get this working.

    Here's what I'm supposed to do.

    •public static Person parsePerson(String parseString, String delim)
    • This method must parse the given parseString according to the given delim, and return a
    new Person object. The method will utilize the modifiers in the Person class to set the features
    of the Person object.

    So, I have everything set up, but I can't get this to compile no matter how I rearrange everything. I've gone over the bicycle example over and over again on the java help page, keep pouring through my book, but nothing helps. For a while, I was able to get this to work a little, until I hit the booleans, and now I'm back farther than I started. Any help as to what I'm doing wrong and what a right way looks like would be greatly appreciated.

    import java.io.*;
    import java.lang.Object;

    public class Person{

    private String name;
    private String gender;
    private String eyeColor;
    private String hairColor;
    private boolean hasGlasses;
    private boolean hasBeard;
    private boolean hasMustache;

    //accessors
    public String getName(){return this.name;}
    public String getGender(){return this.gender;}
    public String getEyeColor(){return this.eyeColor;}
    public String getHairColor(){return this.hairColor;}
    public boolean getHasGlasses(){return this.hasGlasses;}
    public boolean getHasBeard(){return this.hasBeard;}
    public boolean getHasMustache(){return this.hasMustache;}

    //modifiers

    public void setName(){this.name=name;}
    public void setGender(){this.gender=gender;}
    public void setEyeColor(){this.eyeColor=eyeColor;}
    public void setHairColor(){this.hairColor=hairColor;}
    public void setHasGlasses(){this.hasGlasses=hasGlasses;}
    public void setHasBeard(){this.hasBeard=hasBeard;}
    public void setHasMustache(){this.hasMustache=hasMustache;}

    //parse the list of people
    public static Person parsePerson(String parseString, String delim){

    String[] tokens=parseString.split(delim);

    Person.name=tokens[0];
    Person.gender=tokens[1];
    Person.eyeColor=tokens[2];
    Person.hairColor=tokens[3];
    Person.hasGlasses=tokens[4];
    Person.hasBeard=tokens[5];
    Person.hasMustache=tokens[6];



    return parsePerson;
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    It probably won't compile because you're missing a } on the very last line.

    It may also not compile as your setters (setName, setGender, etc.) accept no parameters. For example, public void setName(){this.name=name;} should be public void setName(String name){this.name=name;}.

    Additionally, tokens[4] through tokens[6] are Strings, however you are attempting to set them to boolean values.

    If none of this fixes it, please post the updated code (entire class/file, and in [code] tags please!) and the exact errors + line numbers you get due to compile problems.

  3. #3
    Medieve is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Gave that a shot, tried rearranging a few things, same errors are coming up.

    Java Code:
    import java.io.*;
    import java.lang.Object;
    
    public class Person{
    
    private String name;
    private String gender;
    private String eyeColor;
    private String hairColor;
    private boolean hasGlasses;
    private boolean hasBeard;
    private boolean hasMustache;
    
    public String getName(){return this.name;}
    public String getGender(){return this.gender;}
    public String getEyeColor(){return this.eyeColor;}
    public String getHairColor(){return this.hairColor;}
    public boolean getHasGlasses(){return this.hasGlasses;}
    public boolean getHasBeard(){return this.hasBeard;}
    public boolean getHasMustache(){return this.hasMustache;}
    
    public void setName(String name){this.name=name;}
    public void setGender(String gender){this.gender=gender;}
    public void setEyeColor(String eyeColor){this.eyeColor=eyeColor;}
    public void setHairColor(String hairColor){this.hairColor=hairColor;}
    public void setHasGlasses(boolean hasGlasses){this.hasGlasses=hasGlasses;}
    public void setHasBeard(boolean hasBeard){this.hasBeard=hasBeard;}
    public void setHasMustache(boolean hasMustache){this.hasMustache=hasMustache;}
    
      
    public static Person parsePerson(String parseString, String delim){
      
      String[] tokens=parseString.split(delim);
      
      Person.name=tokens[0];
      Person.gender=tokens[1];
      Person.eyeColor=tokens[2];
      Person.hairColor=tokens[3];
      Person.hasGlasses=tokens[4];
      Person.hasBeard=tokens[5];
      Person.hasMustache=tokens[6];
      
      
    
      return parsePerson;
    }
    }
    Errors are below, and I'm at a complete loss. Everything I can find online and in my book says this SHOULD work.

    Java Code:
    11 errors found:
    File: C:\...\bookClasses\Person.java  [line: 35]
    Error: C:\...\Person.java:35: non-static variable name cannot be referenced from a static context
    File: C:\...\Person.java  [line: 36]
    Error: C:\...\Person.java:36: non-static variable gender cannot be referenced from a static context
    File: C:\...\Person.java  [line: 37]
    Error: C:\...\Person.java:37: non-static variable eyeColor cannot be referenced from a static context
    File: C:\...\Person.java  [line: 38]
    Error: C:\...\Person.java:38: non-static variable hairColor cannot be referenced from a static context
    File: C:\...\Person.java  [line: 39]
    Error: C:\...\bookClasses\Person.java:39: non-static variable hasGlasses cannot be referenced from a static context
    File: C:\...\Person.java  [line: 39]
    Error: C:\...\Person.java:39: incompatible types
    found   : java.lang.String
    required: boolean
    File: C:\...\Person.java  [line: 40]
    Error: C:\...\Person.java:40: non-static variable hasBeard cannot be referenced from a static context
    File: C:\...\Person.java  [line: 40]
    Error: C:\...\Person.java:40: incompatible types
    found   : java.lang.String
    required: boolean
    File: C:\...\Person.java  [line: 41]
    Error: C:\...\Person.java:41: non-static variable hasMustache cannot be referenced from a static context
    File: C:\...\Person.java  [line: 41]
    Error: C:\...\Person.java:41: incompatible types
    found   : java.lang.String
    required: boolean
    File: C:\...\Person.java  [line: 45]
    Error: C:\...\Person.java:45: cannot find symbol
    symbol  : variable parsePerson
    location: class Person

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Ah, alright.

    You have three issues:
    1) You are trying to access Person.variable rather than creating a new Person object, as such:
    Java Code:
    Person someVarName = new Person();
    someVarName.name = tokens[0];
    // etc
    You can read more on that: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    2) Like I said above, tokens[4], tokens[5], and tokens[6] are Strings, but you want those to be booleans. Instead, you will want to check if the value of tokens[4] is "0", and if it is, then use false. Otherwise, true.

    3) You're returning a function, rather than an object. Once you've created your object (as in step 1), you have to return THAT object.

  5. #5
    off99555 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Use Boolean.parseBoolean(String text); to parse String (true or false) into boolean data type catch Exception if cannot parse. So I have made a new method for you.

    public static Person parsePerson(String parseString, String delim){

    String[] tokens=parseString.split(delim);
    Person p = new Person();
    p.name=tokens[0];
    p.gender=tokens[1];
    p.eyeColor=tokens[2];
    p.hairColor=tokens[3];
    p.hasGlasses=Boolean.parseBoolean(tokens[4]);
    p.hasBeard=Boolean.parseBoolean(tokens[5]);
    p.hasMustache=Boolean.parseBoolean(tokens[6]);

    return p;
    }
    I'm a teenage Thai Java programmer who studying in secondary school.
    Practicing English in process ...

Similar Threads

  1. arrays with booleans
    By hoosierfan24 in forum New To Java
    Replies: 24
    Last Post: 10-04-2010, 01:07 AM
  2. Problems concerning Static methods and class.
    By RBNSN in forum New To Java
    Replies: 17
    Last Post: 07-15-2010, 01:11 PM
  3. Problems with Static toString() method
    By RBNSN in forum New To Java
    Replies: 6
    Last Post: 07-09-2010, 10:15 AM
  4. do static methods cause synchronization problems
    By rajakumar.tu in forum Advanced Java
    Replies: 2
    Last Post: 12-24-2009, 07:39 AM
  5. New to this - having errors with static problems
    By afisher300 in forum New To Java
    Replies: 4
    Last Post: 04-20-2009, 11:42 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
  •