How to read a Boolean

W

William

Hi,

I'm having difficulty reading a boolean value from the console using:

oPlayer.setMember(Input.readBoolean(new String ("Is this Person a Club
Member? : "))) ;

public static boolean readBoolean(String sPrompt) throws IOException
{
boolean bMember = false ;
if (sPrompt != null) System.out.print(sPrompt) ;
try
{
bMember = new Boolean(new
StringTokenizer(keyBoard.readLine()).nextToken()).booleanValue() ;
return bMember ;
}
catch (IOException e)
{ /* Mmmm damn it! */ }
finally
{ return bMember ; }

But regardless of what I enter (Y,y,N,n,T,t,F,f) I always end up with a
false value for the oPlayer.m_bMember attribute. I'd appreciate it if
someone could point out to me what I (must) be doing wrong.
 
W

William

Andrew Thompson wrote:

First, what your doing wrong. You are not RTFM.
...

Hi Andrew,

You're right there's no excuse for my asking. So a lesson learned; if in
doubt RTFM. :)
 
A

Andrew Thompson

William said:
Andrew Thompson wrote: ....
You're right there's no excuse for my asking. So a lesson learned; if in
doubt RTFM. :)

Your welcome. ;-)

Remind me if _I_ forget!
 
R

Roedy Green

new Boolean(new
StringTokenizer(keyBoard.readLine()).nextToken()).booleanValue() ;
return bMember ;

You want to expand this code so you can see what is happening.

Read the line, print it.

Get the token, print it.

I was not aware StringTokenizer had a facility for reading booleans.
Check that it indeed does. If you can't get it to work, resort to
reading a string and converting it to boolean yourself.

You are going are a rather indirect route with new
Boolean().booleanValue(). There are usually more direct ways of
converting. See http://mindprod.com/converter.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top