Java question - Urgent

2006-12-15 12:57 am
I find that the Java does not allow the "string and char" to do the comparsion in the following 'if' statement, can anyone tell me how do I re-solve it?

for example,

import javax.swing.*;
public class testing{
public static void main(String[] args){
{
String input1 = JOptionPane.showInputDialog(null, "Pls input A or M", JOptionPane.QUESTION _MESSAGE);

If (input1 == A) {

System.out.println("Auto");

}

}
}

Thanks for any advise.
更新1:

Thank you all. Both answers are help and one more questions, If I want the if statement can allow either "A" or "a" character, how do i do it on the same example, Thanks all.

回答 (3)

2006-12-15 8:09 am
✔ 最佳答案
input1.equalsIgnoreCase(”a”);
2006-12-15 1:13 am
The checking should be:
if (input1 .compareTo("A") == 0)
2006-12-15 1:10 am
what do u mean? if 'A' is a char than "A" is a string. so u can compare string [input] with sting "A".

change ur if statement to:
if ( input.compareTo( "A" ) == 0 )

is it OK?

2006-12-15 09:42:02 補充:
u can try:1. if ( input1.compareTo( "A" ) == 0 || input1.compareTo( "a" ) == 0 )2. if ( input1.toUpperCase( ).compareTo( "A" ) == 0 )


收錄日期: 2021-04-21 12:50:04
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061214000051KK02212

檢視 Wayback Machine 備份