Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Unhandled Exception Type FileNotFoundException

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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
     else inputCommandFile();
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    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)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    Quote Originally Posted by Norm View Post
    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?

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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
     else inputCommandFile();


    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;
     
     
    	}

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    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
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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;
     
     
    	}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    stopped working
    Can you explain what " stopped working" means?

    What happens with the String returned by the inputCommandFile() method?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    Quote Originally Posted by Norm View Post
    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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    Are you talking about another part of the program, not the throws clause?

    the method prevents more than 7 commands being executed.
    How does it do that?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unhandled Exception Type FileNotFoundException

    if (numoflinesread==Maxlines)
    		{
    		break;
    		}
    		return line;
    		}
    		return line;

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default 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?

Similar Threads

  1. [SOLVED] FileNotFoundException on ftp connection
    By hdty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2013, 07:39 PM
  2. Replies: 3
    Last Post: December 2nd, 2012, 11:18 PM
  3. [SOLVED] FileNotFoundException via Scanner
    By mtn_student in forum Exceptions
    Replies: 6
    Last Post: August 31st, 2011, 04:14 PM
  4. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  5. FileNotFoundException!?
    By Shaddam in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 29th, 2010, 06:46 PM