If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > exception with Scanner input that has spaces

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-06-09, 21:47
hally hally is offline
Registered User
 
Join Date: Feb 2003
Location: Toronto, Ontario, Canada
Posts: 34
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());

}
}
}
}
Reply With Quote
  #2 (permalink)  
Old 10-06-09, 22:23
hally hally is offline
Registered User
 
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());
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On