Results 1 to 6 of 6
  1. #1
    t500yo is offline Member
    Join Date
    Jun 2010
    Posts
    14
    Rep Power
    0

    Default 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;
    Java Code:
    if ((extension == null)||(ext == extension.toLowerCase())) {...}
    in this class:
    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;
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    ext == extension.toLowerCase()
    Don't use == to compare the contents of two objects. Most classes have methods to do this. String uses the equals() method.

  3. #3
    t500yo is offline Member
    Join Date
    Jun 2010
    Posts
    14
    Rep Power
    0

    Default

    I guess that was was a very simple question. But I did not know the answer.. :-S
    Thank you, Norm! Now it does work!

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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 Boyo
    If 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!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    the operator == compares the two objects themselves.
    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.
    Because of the way the compiler handles Strings it is sometimes possible that == will work when comparing a String object to a literal ("asdf").

  6. #6
    t500yo is offline Member
    Join Date
    Jun 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  1. Replies: 9
    Last Post: 04-23-2010, 01:52 AM
  2. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  3. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •