Here is my code. The FileNotFoundException is not being thrown for some reason when i give it a fileName that does not exist.
public static String Question1( String fileName )
{
String message = "";
if ( fileName == null )
{
fileName = "files/question1/sample.txt";
}
try
{
Scanner fileScan = new Scanner( new File ( fileName ));
while ( fileScan.hasNext() )
{
String readLine = fileScan.nextLine();
if ( message.equals( "" ) )
{
message = readLine + "\n";
}
else
{
message = message + readLine + "\n";
}
}
}
catch ( FileNotFoundException e )
{
message = "Error: Could not find file!";
}
return message;
}
When i run the code with a fileName that does not exist, the message returned is "" instead of "Error: Could not find file!"