Results 1 to 9 of 9
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default Listing directory contents

    Ok, I have a program that lists the contents of a directory. It works fine when I just give it C:\. But as soon as I give it C:\Program Files, it generates a NullPointerException at the for loop.
    Java Code:
    import java.io.File;
    import java.util.Scanner;
    
    public class DirectoryContents {
      public static void main(String[] args) {
      	Scanner input = new Scanner(System.in);
      	String drive;
      	String path;
      	System.out.print("Please enter the drive letter: ");
      	drive = input.next();
    	System.out.print("Please enter the directory path: ");
    	path = drive + ":" + input.next();
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
    
        for (int i = 0; i < listOfFiles.length; i++) {// this is the line with the error.
          if (listOfFiles[i].isFile()) {
            System.out.println("File " + listOfFiles[i].getName());
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }
      }
    
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Java Code:
      public static void main(String[] args) {
      	Scanner input = new Scanner(System.in);
            ...
      	System.out.print("Please enter the drive letter: ");
      	drive = input.next();
    	System.out.print("Please enter the directory path: ");
    	path = drive + ":" + input.next();
            ...
      }
    So I presume you entered "C" in response to the first prompt, and "Program Files" in response to the second prompt. What will your input.next() return? What will path look like after the assignment? You might want to try adding a System.out.println(path) just after that line to see what is happening.

    -Gary-

  3. #3
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Well, actually, it was \Program Files. But thanks for that. It's only reading c:\Program for some reason. Why is it doing that?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Scanner (Java 2 Platform SE 5.0)

    What is .next() supposed to do in that situation?

    -Gary-

  5. #5
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Okay, here's the new code, but it doesn't ask for input for some reason. You put in the drive, and then it just prints the next statement and act like its done. I don't think I fully understand the nextLine command.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by gandalf5166 View Post
    Okay, here's the new code, but it doesn't ask for input for some reason. You put in the drive, and then it just prints the next statement and act like its done. I don't think I fully understand the nextLine command.
    I think you forgot something. :)

    -Gary-

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Lulz.
    Java Code:
    import java.io.File;
    import java.util.Scanner;
    
    public class DirectoryContents {
      public static void main(String[] args) {
      	Scanner input = new Scanner(System.in);
      	String drive = "";
      	String path;
      	System.out.print("Please enter the drive letter: ");
      	drive = input.next();
    	System.out.println("Please enter the directory path: ");
    	path = input.nextLine();
    	path = drive + ":" + path;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
    
        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
            System.out.println("File " + listOfFiles[i].getName());
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }
      }
    
    }

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Yeah, that's what I thought you'd have. And it is indeed confusing. The .next() method looks for the next token, and considers '\n' to be whitespace, while the .nextLine() method returns everything up to the next '\n'.

    Your code prompts for a drive letter, and you enter "C". The input buffer looks like this:
    Java Code:
    C\n
    Then .next() reads the first token, and the Scanner's pointer is here:
    Java Code:
    C\n
     ^
    Then you are prompted for the path, and you enter "\Program Files". The buffer now looks like this:
    Java Code:
    C\n
     ^
    \\Program Files\n
    So you can see that .next() would return "\Program" while .nextLine() is going to return the remainder of that first line (a zero-length String).

    So you need another .nextLine() after reading the drive letter to clear the buffer. Or else use .nextLine() in the first place to get the drive letter, then use some other logic to make sure you got just one letter, or to take just the first letter from whatever you got.

    Frankly, dealing with input in a console program (and doing it correctly) is a big pain. Typically I like to read a line at a time into a String, and then parse it.

    -Gary-

  9. #9
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    THANK YOU!!! That's really weird though. I put an .nextLine on every one and it was perfect. That's the only problem I was having, so now I'm done with one third of my assignment.

Similar Threads

  1. Replies: 3
    Last Post: 02-10-2010, 08:22 PM
  2. Replies: 8
    Last Post: 11-20-2009, 10:33 PM
  3. opening directory (folder) to reveal contents.
    By solris in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 07:40 PM
  4. Replies: 2
    Last Post: 05-11-2009, 11:07 AM
  5. Listing file system from root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 04-18-2009, 11:03 AM

Posting Permissions

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