1

I am doing a code for my "intro to java" programming course and I ran into an error I don't really know how to fix. I keep having an error that says:

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
                Scanner fileIn = new Scanner(hours);

This is the code:

import java.util.Scanner;

import java.io.File;

public class program2{

    public static void main(String[] args)  { 

        int[][] array = new int [8][7];
        int[] total = new int [array.length];
        int[] finalTotal = new int[array.length];
        int[] employees = {0,1,2,3,4,5,6,7};


        File hours = new File("prog2.dat"); //read file
        Scanner fileIn = new Scanner(hours);//assign hours to array


        while(fileIn.hasNext()){
            for(int i = 0; i < 8; i++) {
                for (int a = 0; a < 7; a++) {
                    int num =fileIn.nextInt();
                        array[i][a] = num;
            }
        }
    }

    fileIn.close(); //Closes file

    // takes employees hour total
    for(int i=0; i < 8; i++) {
        total[i] = array[i][0] + array[i][1] + array[i][2] + 
        array[i][3] + array[i][4] + array[i][5] + array[i][6];
        }

    // takes the hours and sorts from greatest to least
    for(int i= 0; i < 7; i++) {
        int greatest = total[i];

        for(int b = i+1; b < 8; b++) {
            if(total[b] > greatest) {
                int employeeTemp = employees[i];
                employees[i] = employees[b];
                employees[b] = employeeTemp;
                int tempNum = greatest;
                greatest = total[b];
                total[i] = greatest;
                total[b] = tempNum;
            }
        }
    }

    // print employee number and worked hours
    for(int i = 0; i < 8; i++) {
        System.out.println(" Employee #" + employees[i] + ": " +
        total[i]);
        }

    }
}

Please help in any way you can.

4
  • What should I do to have the file to be found? Commented Jul 15, 2014 at 6:43
  • Its not about a missing file. Its a compiler message. Commented Jul 15, 2014 at 6:44
  • seems to be related to this question, not a duplicate Commented Jul 15, 2014 at 6:45
  • haven't they told you at your "intro to java" programming course that using an IDE (like Eclipse at eclipse.org/downloads/index-developer.php ) helps? Commented Jul 15, 2014 at 11:43

5 Answers 5

5

Scanner(fileInstance) declares to throw a FileNotFoundException so you must handle it

    Scanner fileIn = null;
    try{
       fileIn = new Scanner(hours)
    }catch(FileNotFoundException e){
        // write code that handles when this exceptional condition raises
    }

or rethrow it

public static void main(String[] args)  throws FileNotFoudnException { 

Also See

Sign up to request clarification or add additional context in comments.

3 Comments

When I do this, my compiler says it cannot find symbol: public static void main(String[] args) throws FileNotFoundException
you need to import import java.io.FileNotFoundException;
aka just press Shift+Ctrl+O in Eclipse
2

Change definition of your main method to:

public static void main(String[] args)  throws IOException { 

Then read about exceptions in java to understand what does this mean.

2 Comments

After doing this, my compiler is telling me that it cant find the symbol with IOException being the symbol
Add import java.io.IOException; in the beginning of the file. And really, start learning java, not typing some text that looks like a code.
0

The call to Scanner fileIn = new Scanner(hours); may fail if the file is not found. In such a case, a java.io.FileNotFoundException will be thrown. Java is strict in its exception handling - you cannot just ignore this potential exception, but must handle it somehow.

One way of doing this is to add a throws clause to the main method:

public static void main(String[] args) throws FileNotFoundException {

In this case, the program will just crash if the file is not found. A better way, probably, to handle these kind of issues is to explicitly catch the exception:

Scanner fileIn = null;
try {
    File hours = new File("prog2.dat");
    fileIn = new Scanner(hours);
} catch (FileNotFoundException e) {
    System.out.println ("Can't find prog2.dat, quitting");
    System.exit(1);
}

Comments

0

Scanner fileIn = new Scanner(hours);//assign hours to array

This line of code throws an expection FileNotFoundException.

You have two option to solve this problem :

1) Catch the Exception

  Scanner fileIn = null;
        try {
            fileIn = new Scanner(hours);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//assign hours to array

2) Throw the Exception:

 public static void main(String[] args) throws FileNotFoundException  { 

Comments

0

create prog2.dat file and also you enter some hours into that file.

Note: you must save prog2.dat into source directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.