Results 1 to 6 of 6
Thread: String != String (????)
- 06-06-2010, 06:08 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
String != String (????)
Hello people,
I wrote a little class that should set the name of a selected file as text in a JTextField. (check code below) If I want to select a specific type of file (lets say "txt") and I select a "*.txt" file, it goed wrong. It seems like there is a difference between two Strings, whom seem to be equal to me.
Can anybody tell me what I miss?
Its this code that is the error, especially the latter;
in this class:Java Code:if ((extension == null)||(ext == extension.toLowerCase())) {...}
Java Code:package utilities; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JTextField; public class fileChooser { /* Constant objects */ private final static JFileChooser fc = new JFileChooser(); /* Open File and Return Absolute Path ('Path/to/file.fasta') */ public static void openFile(JButton button, JTextField textField, String extension) { int returnVal = fc.showOpenDialog(button); if (returnVal == JFileChooser.APPROVE_OPTION) { fc.setFileSelectionMode(JFileChooser.FILES_ONLY); File file = fc.getSelectedFile(); String ext = getExtension(file); if ((extension == null)||(ext == extension.toLowerCase())) { textField.setText(file.getAbsolutePath()); } else { JOptionPane.showMessageDialog(null, "Please select a file with extension '" + extension + "'."); openFile(button,textField,extension); } } } /* Get Extension of a file */ private static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } }
- 06-06-2010, 06:11 PM #2
Don't use == to compare the contents of two objects. Most classes have methods to do this. String uses the equals() method.ext == extension.toLowerCase()
- 06-06-2010, 06:14 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
I guess that was was a very simple question. But I did not know the answer.. :-S
Thank you, Norm! Now it does work!
- 06-06-2010, 10:18 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I realize this is solved, but I'll give an slightly more detailed explanation than Norm did.
The equals method is used to compare whether the values of two objects are equal. For example, two soccer balls that have the same brand, design, and size could be considered equal when compared by the equals method. Unlike the method, the operator == compares the two objects themselves. This means that even though according to equals, they are the same, the operator would say the soccer balls are different because they are not actually the same object.
For primitive types, there is no equals method, but the == operator for primitive types performs value comparison. However, don't confuse the object wrappers with the primitive types - new Integer(1)==new Integer(1) is false while 1==1 is true. (new Integer(1).equals(new Integer(1)) is true)
Hope this helps.
Singing BoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-06-2010, 11:09 PM #5
Further clarification. == used with two object references (pointers) tests it the two object references point to the same object. IE that the pointers have the same value.the operator == compares the two objects themselves.
Because of the way the compiler handles Strings it is sometimes possible that == will work when comparing a String object to a literal ("asdf").
- 06-07-2010, 10:30 AM #6
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
Hey all,
I guess that I have to adjust to Java. I used to create websites and stuff in javascript, php and some other languages but I never really got used to the idea of objects and pointers. It is useful though, and very handy but sometimes, like in this case, a little too much.
Good to know that you all are here!
Thanks guys!
Similar Threads
-
copying a part of a string and moving it to the end of the string?
By sidd0123 in forum New To JavaReplies: 9Last Post: 04-23-2010, 01:52 AM -
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks