Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?
Code java:
package HeightsSorter;
import java.util.*;
import java.io.*;
public class HeightsSorter {
public static void main(String[] args)
{
try
{
Scanner keyb = new Scanner(System.in);
System.out.print("Enter the file name> ");
String dataFile = keyb.next();
Scanner readFile = new Scanner(new File(dataFile));
System.out.print("Enter the output file name> ");
String outFile = keyb.next();
PrintWriter pw = new PrintWriter(outFile);
int ft,in;
ArrayList<Height> heights = new ArrayList<Height>();
while (readFile.hasNext())
{
ft = readFile.next();
in = readFile.next();
heights.add(new Height(ft, in));
}
readFile.close();
int i;
System.out.println("Unsorted Heights from "+dataFile+":");
for (i=0; i<heights.size();i++){
System.out.println(heights.get(i));
}
Collections.sort(heights);
System.out.println();
System.out.println("Sorted Heights from "+dataFile+":");
for (i=0; i<heights.size();i++)
{
System.out.println(heights.get(i));
pw.println(heights.get(i));
}
pw.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(3);
}
}
}
Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?
Are you typing the file name correctly? Remember that when you give it a file name it has to be exact, so as an example look at the following:
File1 = Wrong
File1.txt = Correct
You also need to make sure the file already exists somewhere in the same folder that the Java project is in.
Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?
It's saved in the same folder as my project and I'm typing it in correctly. It's still giving me the same message. =(
Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?
First off, you should be calling Scanner's nextLine() method not the next() method to be sure that you're handling the end-of-line (EOL) token appropriately. Next, if you get a run-time exception it is most helpful if you post the full exception and let us know which line is throwing the exception. But most important I agree that you're likely not looking in the right place for the file. You should either be entering the full path to the file, starting with the drive letter, or else entering the path to the file relative to the user.dir. To find out what the user directory is, somewhere at the top of your main method put in the line
Code :
System.out.println(System.getProperty("user.dir"));