Results 1 to 5 of 5
- 12-07-2010, 08:33 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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;
}
- 12-07-2010, 08:40 PM #2
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.
- 12-08-2010, 12:25 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Gave that a shot, tried rearranging a few things, same errors are coming up.
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: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; } }
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
- 12-08-2010, 03:31 AM #4
Ah, alright.
You have three issues:
1) You are trying to access Person.variable rather than creating a new Person object, as such:
You can read more on that: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)Java Code:Person someVarName = new Person(); someVarName.name = tokens[0]; // etc
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.
- 12-08-2010, 06:07 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
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
-
arrays with booleans
By hoosierfan24 in forum New To JavaReplies: 24Last Post: 10-04-2010, 01:07 AM -
Problems concerning Static methods and class.
By RBNSN in forum New To JavaReplies: 17Last Post: 07-15-2010, 01:11 PM -
Problems with Static toString() method
By RBNSN in forum New To JavaReplies: 6Last Post: 07-09-2010, 10:15 AM -
do static methods cause synchronization problems
By rajakumar.tu in forum Advanced JavaReplies: 2Last Post: 12-24-2009, 07:39 AM -
New to this - having errors with static problems
By afisher300 in forum New To JavaReplies: 4Last Post: 04-20-2009, 11:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks