Results 1 to 12 of 12
  1. #1
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Getting Compile Error Of String can not be converted to List<String>

    I am recursively scanning a directory and capturing essentially everything after the last "." in the file name. I am using a HashTable so I can then iterate it to get the unique values only. However, in my code this line
    Java Code:
    extensionsInDir = file.getName().substring(file.getName().lastIndexOf(".") + 1);
    Gives me the error -> String can not be converted to List<String>

    This is my full code - how should I re-write or re-factor in order for this to execute succesfully?

    Java Code:
        Set<String> uniqueExtension;
        List<String> extensionsInDir;
    	
    	public void showFiles(File[] files) {
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("Directory: " + file.getName());
                    showFiles(file.listFiles());
                } else { 
                    System.out.println("File: " + file.getName()); 
                    extensionsInDir = file.getName().substring(file.getName().lastIndexOf(".") + 1);
                    uniqueExtension = new HashSet<String>(extensionsInDir);
                }
            }
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    String can not be converted to List<String>
    That would look like the code is trying to assign a String value to a List variable.
    That won't work.
    What are you trying to do?

    Using the List's add method would work.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Quote Originally Posted by Norm View Post
    That would look like the code is trying to assign a String value to a List variable.
    That won't work.
    What are you trying to do?

    Using the List's add method would work.
    I am recursively scanning a directory and writing the file contents to the window, and what I want to do with the line of code that is giving me an issue is to assign the "extension" everything after the last "." to an array, to then get a unique list of the "extensions"

    Does that help clarify? How would I use the Lists.add method in my scenario?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,192
    Rep Power
    12

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    The right side of line 11 returns a String. The problem is that you are trying to assign a String object
    to a List<String> type. You need to simply get the String and add it to the list using extensionsInDir.add() method.

    If I had an array of Files, I could do the following to add their names to a HashSet.

    Java Code:
    List<String> set = new HashSet<String>();
    for (File f: files) {
          set.add(f.getName());
    }
    Not certain what you are trying to do at lines 11 and 12.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Quote Originally Posted by jim829 View Post
    The right side of line 11 returns a String. The problem is that you are trying to assign a String object
    to a List<String> type. You need to simply get the String and add it to the list using extensionsInDir.add() method.

    If I had an array of Files, I could do the following to add their names to a HashSet.

    Java Code:
    List<String> set = new HashSet<String>();
    for (File f: files) {
          set.add(f.getName());
    }
    Not certain what you are trying to do at lines 11 and 12.

    Regards,
    Jim
    Line 11 - I am capturing everything to the right of the last "." of the filename and assigning it to extensionsInDir
    Line 12 - I am using a HashSet to get a list of UNIQUE values in extensionsInDir

    If I follow, if I just add a new loop and run
    Java Code:
    set.add(f.getName());
    I would be adding full file names, not just everything to the right of the last "." correct?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    I would be adding full file names,
    Print out the value of f.getName() to see what is being added.
    Or print out the value of set to see what is in it.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,192
    Rep Power
    12

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    My example was just an example of how to add to a list. You need to grab the substring you want from the file name and
    put that in the list. I do not believe you need to be creating a new list in the loop. One should suffice. But your list will
    have the following characteristics.

    1. for a complete hierarchy of files, only a single copy of repeated strings will be included in the set (that is the nature of a set - to not contain duplicates).
    2. By using a single List for a directory hierarchy you will lose the hierarchical relationship. That is, your list will be flat.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Quote Originally Posted by Norm View Post
    Print out the value of f.getName() to see what is being added.
    Or print out the value of set to see what is in it.
    If I print it out - I get the extension of every file in the folder and sub-folders. Which is part 1 of what I am after. Part 2 - my desired result is to get UNIQUE values only
    Java Code:
       Set<String> uniqueExtension;
       
        uniqueExtension = new HashSet<String>();
    	for (File f: files) {
    		  System.out.println(file.getName().substring(file.getName().lastIndexOf(".") + 1));
    	}
    will give me (for example)
    form
    form
    java
    java
    txt
    txt

    I only want
    form
    java
    txt

    One instance.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    If they are added to a Set, there will only be one copy kept in the Set.

    The posted code prints every value that it gets inside the loop. The Set is not used.
    Add the values to a Set where only one will be saved.
    Then print the Set to see there is only one.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Quote Originally Posted by Norm View Post
    If they are added to a Set, there will only be one copy kept in the Set.

    The posted code prints every value that it gets inside the loop. The Set is not used.
    Add the values to a Set where only one will be saved.
    Then print the Set to see there is only one.
    But then I am back to my initial issue of how to assign a List<string> to a Set or HashTable

    ammendment ->
    I tried this code
    Java Code:
    Set<String> uniqueExtension;
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    	chooser = new JFileChooser(); 
    	chooser.setCurrentDirectory(new java.io.File("."));
    	chooser.setDialogTitle(choosertitle);
    	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    	chooser.setAcceptAllFileFilterUsed(false);
    	if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
    		File[] files = chooser.getSelectedFile().listFiles();
    		showFiles(files);
    		System.out.println("Extensions: " + uniqueExtension);
    	} else { System.out.println("No Selection "); }
    }  
    
    public void showFiles(File[] files) {
    	for (File file : files) {
    		if (file.isDirectory()) {
    			showFiles(file.listFiles()); // Calls same method again.
    		} else { 
    			for (File f: files) {
    				  uniqueExtension.add(f.getName().substring(f.getName().lastIndexOf(".") + 1));
    			}
    		}
    	}
    }
    but it gives me a runtime error -- short version of
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Last edited by juan4412; 03-04-2018 at 02:11 AM.

  11. #11
    juan4412 is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Please disregard my above. I realized I was not instantiating properly. I had omitted
    Java Code:
    uniqueExtension = new HashSet<String>();

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Getting Compile Error Of String can not be converted to List<String>

    Is it working now?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Jtextfield cannot be converted to string
    By madtaffy in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2015, 09:07 PM
  2. Help with separating a String to be converted to a Char
    By AnnaPatricia in forum New To Java
    Replies: 10
    Last Post: 08-16-2012, 05:49 PM
  3. Replies: 2
    Last Post: 07-05-2012, 08:10 AM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. Replies: 4
    Last Post: 03-25-2008, 12:16 PM

Posting Permissions

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