Unhandled Exception Type FileNotFoundException
Hi there. I'm trying to do a file reader which is accessible via a menu system but i keep getting the error “unhandled exception type FileNotFoundException” at the
Code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class RobotController1 {
private static Finch myf = null;
public static void main(String[] args) {
myf = new Finch();
String lastCommand = null;
int count =1;
for (String input = getInput(); !input.equals("End Program"); input = getInput())
{
if (!input.equals(lastCommand))
{
if (input.equals("Forward")) moveForward();
else if (input.equals("Back")) moveBack();
else if (input.equals("Left Turn")) moveLeft();
else if (input.equals("Right Turn")) moveRight();
else if (input.equals("Nose Colour")) greenLED();
else if (input.equals("Three Point Turn")) threePointTurn(); //Three point turn
else inputCommandFile();
lastCommand = input;
System.out.println(count);
}
else
{
JOptionPane.showMessageDialog(null, input + " cannot be chosen directly after " + lastCommand,"ERROR",0);
}
}
while (count <8);
System.out.println("Finch Disconnected...");
myf.quit();
}
private static String getInput() {
Object[] possibilities = {"Forward","Back","Left Turn","Right Turn", "Nose Colour" ,"Three Point Turn","Input Command File","End Program"};
String c = (String)JOptionPane.showInputDialog(null,"Robot Controller \n\nChoose a command:\n\n","Robot Controller",JOptionPane.PLAIN_MESSAGE, null,possibilities,"Forward");
if (c == null || c.length() == 0)
c = "End Program";
return c;
}
private static String inputCommandFile(String s) throws FileNotFoundException
{
File file = new File("h:\\My Documents\\Inputfile.txt");
Scanner sc = new Scanner(new FileInputStream(file));
int numoflinesread=0;
int Maxlines=7;//maximum amount of commands it can read is 7
String line = null;
while(sc.hasNextLine())
{
line = sc.nextLine();
if (line.indexOf("Nose") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Forward ") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Backward") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Left") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Right") > -1)
{
stringconversion(line);
}
numoflinesread++;
if (numoflinesread==Maxlines)
{
break;
}
return line;
}
return line;
}
Please help me, I've watched tutorials etc. but I'm at my wits end now. Thank you :)
Re: Unhandled Exception Type FileNotFoundException
Code :
private static String inputCommandFile(String s) throws FileNotFoundException
That method is defined to throw an exception. Any code that calls that method must be ready to handle the exception either with a try{}catch block or a further throws clause.
See the tutorial:
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Re: Unhandled Exception Type FileNotFoundException
Quote:
Originally Posted by
Norm
Code :
private static String inputCommandFile(String s) throws FileNotFoundException
That method is defined to throw an exception. Any code that calls that method must be ready to handle the exception either with a try{}catch block or a further throws clause.
See the tutorial:
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
do i add the catch onto the method?
Unhandled Exception Type FileNotFoundException
Hi there. I'm trying to do a file reader which is accessible via a menu system but i keep getting the error “unhandled exception type FileNotFoundException” at the
Code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class RobotController1 {
private static Finch myf = null;
public static void main(String[] args) {
myf = new Finch();
String lastCommand = null;
int count =1;
for (String input = getInput(); !input.equals("End Program"); input = getInput())
{
if (!input.equals(lastCommand))
{
if (input.equals("Forward")) moveForward();
else if (input.equals("Back")) moveBack();
else if (input.equals("Left Turn")) moveLeft();
else if (input.equals("Right Turn")) moveRight();
else if (input.equals("Nose Colour")) greenLED();
else if (input.equals("Three Point Turn")) threePointTurn(); //Three point turn
else inputCommandFile();
lastCommand = input;
System.out.println(count);
}
else
{
JOptionPane.showMessageDialog(null, input + " cannot be chosen directly after " + lastCommand,"ERROR",0);
}
}
while (count <8);
System.out.println("Finch Disconnected...");
myf.quit();
}
private static String getInput() {
Object[] possibilities = {"Forward","Back","Left Turn","Right Turn", "Nose Colour" ,"Three Point Turn","Input Command File","End Program"};
String c = (String)JOptionPane.showInputDialog(null,"Robot Controller \n\nChoose a command:\n\n","Robot Controller",JOptionPane.PLAIN_MESSAGE, null,possibilities,"Forward");
if (c == null || c.length() == 0)
c = "End Program";
return c;
}
private static String inputCommandFile(String s) throws FileNotFoundException
{
File file = new File("h:\\My Documents\\Inputfile.txt");
Scanner sc = new Scanner(new FileInputStream(file));
int numoflinesread=0;
int Maxlines=7;//maximum amount of commands it can read is 7
String line = null;
while(sc.hasNextLine())
{
line = sc.nextLine();
if (line.indexOf("Nose") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Forward ") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Backward") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Left") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Right") > -1)
{
stringconversion(line);
}
numoflinesread++;
if (numoflinesread==Maxlines)
{
break;
}
return line;
}
return line;
}
Re: Unhandled Exception Type FileNotFoundException
Quote:
do i add the catch onto the method?
Did you read the tutorial? What you just asked does not make sense.
catch goes with the try statement.
methods use the throws clause
Re: Unhandled Exception Type FileNotFoundException
Ok so I did it, it worked but then randomly stopped working even though I made no changes to the correct version
Code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class RobotController1 {
private static Finch myf = null;
public static void main(String[] args) throws FileNotFoundException {
myf = new Finch();
String lastCommand = null;
int count =1;
for (String input = getInput(); !input.equals("End Program"); input = getInput())
{
if (!input.equals(lastCommand) || input.equals("Left Turn")|| input.equals("Right Turn") || input.equals("Nose Colour") )
{
if (input.equals("Forward")) moveForward();
else if (input.equals("Back")) moveBack();
else if (input.equals("Left Turn")) moveLeft();
else if (input.equals("Right Turn")) moveRight();
else if (input.equals("Nose Colour")) greenLED();
else if (input.equals("Three Point Turn")) threePointTurn(); //Three point turn
else inputCommandFile(input);
lastCommand = input;
System.out.println(count);
}
else
{
JOptionPane.showMessageDialog(null, input + " cannot be chosen directly after " + lastCommand,"ERROR",0);
}
}
while (count <8);
System.out.println("Finch Disconnected...");
myf.quit();
}
private static String inputCommandFile(String s) throws FileNotFoundException
{
File file = new File("C:\\Users\\Omar\\Desktop\\Inputfile.txt");
Scanner sc = new Scanner(new FileInputStream(file));
int numoflinesread=0;
int Maxlines=7;//maximum amount of commands it can read is 7
String line = null;
while(sc.hasNextLine())
{
line = sc.nextLine();
if (line.indexOf("Nose") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Forward ") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Backward") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Left") > -1)
{
stringconversion(line);
}
else if (line.indexOf("Right") > -1)
{
stringconversion(line);
}
numoflinesread++;
if (numoflinesread==Maxlines)
{
break;
}
return line;
}
return line;
}
Re: Unhandled Exception Type FileNotFoundException
Can you explain what " stopped working" means?
What happens with the String returned by the inputCommandFile() method?
Re: Unhandled Exception Type FileNotFoundException
Quote:
Originally Posted by
Norm
Can you explain what " stopped working" means?
What happens with the String returned by the iputCommandFile() method?
Basically, I have a Finch robot which I'm programming. The method reads commands to move the Finch from a file. Also the method prevents more than 7 commands being executed.
Re: Unhandled Exception Type FileNotFoundException
Are you talking about another part of the program, not the throws clause?
Quote:
the method prevents more than 7 commands being executed.
How does it do that?
Re: Unhandled Exception Type FileNotFoundException
Code :
if (numoflinesread==Maxlines)
{
break;
}
return line;
}
return line;
Re: Unhandled Exception Type FileNotFoundException
What is that posted code supposed to show?
Why the if statement when the results looks like the code always does a return line?
Re: Unhandled Exception Type FileNotFoundException
Look at your inputCommandFile method. You still have it defined to throw an exception but never use a try-catch block or throw anywhere in it.
Your most recent snipped of code breaks out of the while-loop but I think it would always execute return line. In other words, no matter if the condition in the if-statement evaluates to true or false, it's still going to return the same code. Is that intentional?