• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to Boolean ??

 
Sonu Shawnee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone see is theres anything wrong with this code ??
public boolean setQuestionData(int q, Object data) {
// Check data type
Boolean value;

if(data instanceof Boolean) {
value = (Boolean)data;
}

else if(data instanceof String) {
//heres where I am not very sure
value = Boolean.valueOf((String)data);
}
else {
throw new IllegalArgumentException("data must be either an Boolean " +
"or a String");
}
Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try it?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Sonu S"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Sonu Shawnee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get errors but I dunno if the problem is due to this,
TokenTest.java:187: cannot resolve symbol
symbol : method getTestScore ()
location: class georgetown.csl.projects.npdp.tests.tokentest.TokenTestData
return data.getTestScore().toString();
Now the TestScore method looks like this,
public String getTestScore() {
return data.getTestScore().toString();
}
Thanks.
 
Sonu Shawnee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok Jessica !!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean aside from that fact that it does not return anything?
Or the fact that it is working in Boolean, but says that it will return a boolean?
And the comment should use "a Boolean" not "an Boolean" (I know - picky picky )
Other than that - looks good .
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - just saw your additional post.
Why don't you post the complete code that is acting up.
 
Sonu Shawnee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but there are a lot of other classes that I implement extend all of which are not mine, but I can definitely get to them if I want to.
Also, I am trying to implement the mvc design pattern.
Another question. Is there something like a MethodNotFoundException which I can throw instead of doing a,
method someMethod() {
return false;
}

package georgetown.csl.projects.npdp.tests.tokentest;
import georgetown.csl.tests.ScoredTest;
import georgetown.csl.projects.npdp.tests.tokentest.TokenTestData;
/**
* Test class for the Children's Token Test. This test consists
* of 61 questions that are answered in the form of a "Yes/No" depending on
* whether the subject offered the correct response to the examiner's question.
*
* @see georgetown.csl.projects.npdp.tests.tokentest.TokenTestData
* @see georgetown.csl.tests.ScoredTest
* author Sonu
* @version 1.0, 07/26/2002
*/
public class TokenTest implements ScoredTest {
/**
* Constructor
*/
public TokenTest() {
// Initialize the data
data = new TokenTestData();
// Set the Validation Variable(s)
scoreCurrent = false;
}
/**
* Constructor: Value
*/
public TokenTest(TokenTestData d) {
// Initialize the data
data = d;
// Set the validation variable
scoreCurrent = false;
}

//-------------------------------\\
// ScoredTest Methods: Scoreable \\
//-------------------------------\\
/**
* Tries to score this test data.
* The scoring for this test has not been implemented, hence the method
* will always return false, i.e. return without doing anything
*
* @return <CODE>true</CODE> if this test was successfully scored,
* <CODE>false</CODE> if scoring fails for any reason
*/
public boolean score() {
return false;
}
/**
* Checks the test information to see if this test can be scored.
*
* @return <CODE>true</CODE> if all information that is needed for scoring
* is present, meaning <CODE>score</CODE> can be called,
* <CODE>false</CODE> if all information is not present or valid
* @see #score()
*/
public boolean canScore() {
// Check to make sure that data has been initialized and is valid
if(!data.isInitialized() || !data.isValid())
return false;
// Data is good
return true;
}
/**
* Checks to see if the score currently stored is current.
*
* @return <CODE>true</CODE> if the score is upto date, <CODE>false</CODE>
* the score is not upto date and <CODE>score</CODE> should be called
*/
public boolean scoreCurrent() {
return scoreCurrent;
}

//--------------------------\\
// ScoredTest Methods: Test \\
//--------------------------\\
/**
* Converts the provided <CODE>data</CODE> to an acceptable type that can
* be stored. If successful <CODE>score</CODE> may need to be called.
*
* @param q Number of the question to set
* @param data An object of to set the data to
* @return <CODE>true</CODE> if the data was successfully stored,
* <CODE>fales</CODE> if otherwise
* @exception java.lang.IllegalArgumentException
* Thrown if <CODE>q</CODE> or <CODE>data</CODE> are invalid
* in anyway. <CODE>data</CODE> can be invalid if it is not
* a <CODE>String</CODE> or <CODE>Boolean</CODE> object.
*/
public boolean setQuestionData(int q, Object data) {
// Check data type
Boolean value;
if(data instanceof Boolean) {
value = (Boolean)data;
}
else if(data instanceof String) {
value = Boolean.valueOf((String)data);
}
else {
throw new IllegalArgumentException("data must be either an Boolean " +
"or a String");
}

// Set the question.
this.data.setQuestions(q, value);
scoreCurrent = false;
return true;
}
/**
* Retrieves the data for the question number specified. The result is
* returned as a <CODE>String</CODE> but cast as an <CODE>Object</CODE>.
*
* @param q Number of the question to get
* @return a <CODE>String</CODE> version of the saved data number, to use
* this method you must explicitally declare the result as a
* <CODE>String</CODE>
* @exception java.lang.ArrayIndexOutOfBoundsException
* if the <CODE>index</CODE> was invalid.
*/
public Object getQuestionData(int q) {
return data.getQuestions(q);
}

//---------------------------\\
// Test Data Methods: Setter \\
//---------------------------\\
/**
* Takes a valid <CODE>TokenTestData</CODE> value bean to set the test data.
*
* @param d A <CODE>TokenTestData</CODE> Object use to set the
* data to
* @exception java.lang.IllegalArgumentException
* Thrown if <CODE>d</CODE> contains invalid data.
*/
// can be taken off - Nishant
public void setTestData(TokenTestData d) {
// Check for valid data
if(!d.isValid())
throw new IllegalArgumentException("d must be valid TokenTestData");
// Set valid data
data = d;
scoreCurrent = false;
}

//---------------------------\\
// Test Data Methods: Getter \\
//---------------------------\\
/**
* Packages the test data into a <CODE>TokenTestData</CODE> object.
*
* @return a <CODE>TokenTestData</CODE> object containing the test data
*/
public TokenTestData getTestData() {
return data;
}
/**
* Retrieves the last calculated test score as a <CODE>String</CODE>.
*
* @return the test score as a <CODE>String</CODE> object
*/
public String getTestScore() {
return data.getTestScore().toString();
}

//----------------------- \\
// Token Test Data Fields \\
//------------------------\\
/**
* The answer data
*/
protected TokenTestData data;

//-------------------------------------------\\
// Token Test Data Fields: Validation Fields \\
//-------------------------------------------\\
/**
* Boolean representing the state of whether the score stored is
* current/up-to-date or not
*
* @see #scoreCurrent()
*/
protected boolean scoreCurrent;
}

Thanks.
 
Sonu Shawnee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it !!
It was just a method in the parent class that I was trying to implement.
I guess the problem had more to do with me not understanding other's parent classes.
Sorry and no guanrantee that I wont make it again !!
Thanks.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah - well, if we are all PERFECT then JavaRanch would probably not exist .
Congrats on finding your problem .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic