Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2003
    Location
    Toronto, Ontario, Canada
    Posts
    34

    Unanswered: exception with Scanner input that has spaces

    Hi

    I've been trying to figure this out but I just can't seem to get it working. My program searches through directories based on user input. When I was hard coding the directory I had no problem but I have now decided to use the Scanner class to accept user input and now I receive a null pointer exception. When the directories do not contain spaces I have no problem. I'll attach a copy of the code.

    Thanks

    hally

    import java.io.File;
    import java.util.*;

    public class DirectoryLookup {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter directory: ");
    File path = new File(input.next());

    //File path = new File("C:\\Documents and Settings\\hally");

    System.out.println(path);

    File[] FilesInDir = path.listFiles();

    if (FilesInDir.length == 0 ){
    System.out.println("No Files exsits in "+path+" directory");
    }
    else {
    for (int i=0; i<FilesInDir.length; i++) {
    System.out.println(FilesInDir[i].getName().toString());

    }
    }
    }
    }

  2. #2
    Join Date
    Feb 2003
    Location
    Toronto, Ontario, Canada
    Posts
    34
    I just figured it out instead, of:
    File path = new File(input.next());
    use this,
    File path = new File(input.nextLine());

Posting Permissions

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