Results 1 to 3 of 3
- 12-06-2008, 06:43 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
comparing inputting strings from Joptionpane and if statement
Is there anyway i can compare strings that i've enter using the JoptionPane. This is what i'm doing.
class test{
public static void main(String[] arguments)
{
String st;
String st2 = "Exit";
st = JOptionPane.showInputDialog("Enter exit to quit program");
//Type exit
if(st == st2){
System.out.print("Exit program");
}else{
//Do something
}
}
}
I'm using the Eclipse Java Compiler 0.883_R34x, 3.4.1 under fedora 10 linux. Thank you for your time
-
Search this forum (or any Java forum) on comparing Strings and you'll get an eye-full as it is one of the more common and persisting problems that we see, especially in new Java programmers. A key is that you don't want to use == to compare them but rather want to use the equals method.
this:means that the two String objects, the one referred by st and the one referred by st2 are one and the same, but that's not what we're interested in here. Instead we want to know if the strings held by these two objects are the same, and that's why you must use equals or equalsIgnoreCase:Java Code:if (st == st2)
Java Code:if (st.equalsIgnoreCase(st2))
Last edited by Fubarable; 12-06-2008 at 06:50 PM.
- 12-06-2008, 06:54 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Problem Comparing Strings (its not what you think)
By hilather in forum New To JavaReplies: 7Last Post: 11-19-2008, 06:43 PM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
JSTL -- Comparing two strings for equality
By trinkets in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-12-2008, 04:39 PM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:44 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks