Hi, I'm trying to learn using Scanner to analyse a file. The running of the program does not work. I have included below code, directory structure and out. Could someone help please. I am using Netbeans.
*****************************************************************************************
************************************ The Code *******************
******************************************************************************************
//use this for testing purposes
File f = new File("C:\\Installs\\Java\\Workspace\\NetBeans\\" +
"Projects\\MyProjects\\src\\packagetwo\\TestDirectory");
//is this main path a directory
if (f.isDirectory()){
System.out.println("");
System.out.println("");
System.out.println("it is a directory");
fileList = f.list();
for(int i=0;i<fileList.length;i++){
File firstFile = new File(fileList[i]);
System.out.println(firstFile.toString());
try{
if (!(firstFile.isDirectory())){
System.out.println("it is not a directory");
Scanner scannedFile = new Scanner(firstFile);
System.out.println("done");
}
if ((firstFile.isDirectory())){
String name = firstFile.toString();
System.out.println(name + "is a directory .... " + "\n");
}
*****************************************************************************************
************************************ The Directory Structure of the test file *******************
******************************************************************************************
it is a directory //this is just a test statement output - main directory
SelfEmployedA.txt //this is just a test statement output - found first file
it is not a directory //this is just a test statement output - confirming it is not a directory
java.io.FileNotFoundException: SelfEmployedA.txt (The system cannot find the fil
e specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at packagetwo.TSD2.main(TSD2.java:43)
TestDirectoryOne
it is not a directory
java.io.FileNotFoundException: TestDirectoryOne (The system cannot find the file
specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at packagetwo.TSD2.main(TSD2.java:43)
Thank you for the reply. I have placed a 'SelfEmployedA.txt' file in the same directory from which I am running the application. It also does not seem to want to work. I really am baffled.
//this is the Application program
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TSD2.java
//this is the file in the same directory
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\SelfEmployedA.txt
//these are picked up in the hard coded directory in the program (File f = new File("C:\\Installs\\..........\\TestDirectory"))
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\SelfEmployedA.txt
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\TestDirectoryOne
john smith wrote:Thank you for the reply. I have placed a 'SelfEmployedA.txt' file in the same directory from which I am running the application. It also does not seem to want to work. I really am baffled.
//this is the Application program
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TSD2.java
//this is the file in the same directory
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\SelfEmployedA.txt
...
Nope, the program gets executed from "C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects" which is where your 'SelfEmployedA.txt' file should be.
If you are facing that sort of problem, turn NetBeans off and use a simple class called from the command line.
Beware: when you call nextInt and enter "123return" the Scanner will interpret the "123" bit as the int and the return as a line which is blank. If you go back through this forum you will find at least two threads in the last week where people have problems with Scanners: try here and here (towards the bottom of the page) and maybe even here, and see whether there is anything helpful there.