JOption Pane/ How to check string

M

Miller_Man

I am new to Java and my teacher wants us to use the JOptionPane to input
data. It appears to only return a string. When I try to check in if
statement I get errors. I figured out how to change a (string)number to
an integer with parseInt but I can't convert string to a char to check in
if statement.
ie: if (answer == 'Y') gives me error string/ char mismatch

Any help would be great.
Thanks
 
A

Andrew Thompson

I am new to Java

c.l.j.help is thisaway..
..and my teacher wants us to use the JOptionPane to input
data. It appears to only return a string.

You can provide JOptionPane any arbitrary Object
<http://java.sun.com/j2se/1.4.2/docs...eDialog(java.awt.Component, java.lang.Object)>

"You can supply a JPanel with JTextField, JTextArea, JComboBox
etcetera and simply query the state of the fields when the JOP returns.
..When I try to check in if
statement I get errors. I figured out how to change a (string)number to
an integer with parseInt but I can't convert string to a char to check in
if statement.
ie: if (answer == 'Y') gives me error string/ char mismatch

'Y' is the way to represent a char, to represent a String, use..
"Y". However, the way that you are comparing strings need fixing as well.

When comparing Strings, always use..

if ( answer.equals("Y") ) {
....

HTH
 
B

Brock Heinz

Andrew Thompson said:
c.l.j.help is thisaway..


You can provide JOptionPane any arbitrary Object
<http://java.sun.com/j2se/1.4.2/docs...eDialog(java.awt.Component, java.lang.Object)>

"You can supply a JPanel with JTextField, JTextArea, JComboBox
etcetera and simply query the state of the fields when the JOP returns.


'Y' is the way to represent a char, to represent a String, use..
"Y". However, the way that you are comparing strings need fixing as well.

When comparing Strings, always use..

if ( answer.equals("Y") ) {
....

This is a bit 'nit picky', but I'd actually advise checking to see if
'Y' equals 'answer'. Doing this:

if ("Y".equals(answer)) {
......

protects you from a null pointer exception.

Brock
 
F

Fahd Shariff

Use the charAt method to get chars from a String.

String s = "YES" ;
char c = s.charAt(0) ; // returns the first character
if(c=='Y') ...

or compare the string directly:
if(s.equals("Y"))...
or
if(s.startsWith("Y"))...

Look in the java api for many more String methods.
 
A

Andrew Thompson

Andrew Thompson wrote in message news:...

This is a bit 'nit picky', ..
(snip better colution)

Be 'nit picky' as it encourages others to give excellent, rather than
merely 'will do' answers.

OTOH, please also trim excess quoted material from your responses.
It highlights the important bits, and saves us all bandwidth.
 
M

Miller_Man

I just want to thank all who posted a reply.
I figured I might get laughed at because I knew it was
a simple question. You all were very polite and informative.
Thanks so much.
 
M

Miller_Man

I just want to thank all who posted a reply.
I figured I might get laughed at because I knew it was
a simple question. You all were very polite and informative.
Thanks so much.
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top