How can I convert a String into boolean?

I am facing difficulty in converting String into boolean.

For example, I have a few variable which i need user to input yes or no as the answer. But I uses string instead. Is there a way which i can ask them to input boolean directly?

Please advise...

credit = JOptionPane.showInputDialog("Enter Yes or No for Credit Satisfactory : ");

System.out.println("The answer for Credit Satisfactory : "+credit);

e_invtry=JOptionPane.showInputDialog("Enter Yes or No for inventry level");

System.out.println("The answer for Quantity Order : "+e_invtry);

Message was edited by:

SummerCool

[635 byte] By [SummerCoola] at [2007-11-27 11:17:13]
# 1

int answer = JOptionPane.showConfirmDialog(null, "Is credit satisfactory?", "Credit satisfactory?", JOptionPane.YES_NO_OPTION);

boolean yes = answer == JOptionPane.YES_OPTION;

dwga at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 2

No, user input is always a String (through the console or text-boxes, that is). You can parse the String to a primitive using their wrapper class, or you can use a simple for statement to compare the String to some other String:

if("y".equalsIgnoreCase(userInput)) {

// ...

}

Edit: didn't even look at the unformatted code, but when using Dialog-boxes, then do as dwg suggested.

prometheuzza at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 3

> you can use a simple for statement to compare the

> String to some other String:

I guess you meant if statement.

dwga at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 4

> > you can use a simple for statement to compare the

> > String to some other String:

>

> I guess you meant if statement.

Oops, indeed!

; )

prometheuzza at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks...but I don't get it....I tried to use your suggestion but i got the message that " cannot find symbol method showConfirmDialog(java.lang.String,int)" ?

Please advise.

The code I use was :

int credit = JOptionPane.showConfirmDialog("Enter Yes or No for credit satisfactory", JOptionPane.YES_NO_OPTION);

SummerCoola at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 6

> Thanks...but I don't get it....I tried to use your

> suggestion but i got the message that " cannot find

> symbol method

> showConfirmDialog(java.lang.String,int)" ?

>

> Please advise.

>

> The code I use was :

>

> int credit = JOptionPane.showConfirmDialog("Enter Yes

> or No for credit satisfactory",

> JOptionPane.YES_NO_OPTION);

Well that was not the example I gave you.

JOptionPane has no method showConfirmDialog that receives a String and an int (exactly what the error message is telling you).

What was wrong with the version I showed you?

dwga at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...
# 7

> Thanks...but I don't get it....I tried to use your

> suggestion but i got the message that " cannot find

> symbol method

> showConfirmDialog(java.lang.String,int)" ?

>

> Please advise.

>

> The code I use was :

>

> int credit = JOptionPane.showConfirmDialog("Enter Yes

> or No for credit satisfactory",

> JOptionPane.YES_NO_OPTION);

Ever heard of copy and paste?

; )

prometheuzza at 2007-7-29 14:23:40 > top of Java-index,Java Essentials,Java Programming...