1

I am attempting toextrac t tokens from a text file using a scanner, the name of the text file is "ElectricToolData.txt",

contents of the text file:

// this is a comment, any lines that start with //
// (and blank lines) should be ignored

// data is rechargeable, power, toolName, itemCode, timesBorrowed, onLoan, cost, weight
true,18V,Makita BHP452RFWX,RD2001,12,false,14995,1800
true,10.8V,Flex Impact Screwdriver FIS439,RD2834,14,true,13499,1200     
false,1350W,DeWalt D23650-GB Circular Saw, RD6582,54,true,14997,5400
false,1500W,Milwaukee DD2-160XE Diamond Core Drill,RD4734,50,false,38894,9000
true,10.8V,Bosch GSR10.8-Li Drill Driver,RD3021,25,true,9995,820
 false,900W,Bosch GSB19-2REA Percussion Drill,RD8654,85,false,19999,4567
true,10.8V,Flex Impact Screwdriver FIS439, RD2835,14,false,13499,1200 
true,18V,DeWalt DW936 Circular Saw,RD4352,18,false,19999,3300 
false,2100W,Sparky FK652 Wall Chaser,RD7625,15,false,29994,8400

Below is my attempt at trying to extract tokens from a text file using a scanner which has been unsuccessful:

Error message (line Scanner scanner): unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown.

public void extractTokens()
    {
        //extracts tokens from the text file
        File text = new File("E:/LEWIS BC 2/project 1/ElectricToolData.txt");

       Scanner scanner = new Scanner(text);

       String toolName = scanner.next();
       String itemCode = scanner.next();
       String power = scanner.next();
       String timesBorrowed = scanner.next();
       String onLoan = scanner.next();
       String cost = scanner.next();
       String weight = scanner.next();

       //System.out.println(parts.get(1)); // "en"
    }

Any replies or help would be greatly appreciated as I am really confused..

  • You must either `catch` the exception that `new Scanner(File)` [throws](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)), or declare that you are throwing it yourself. –  Mar 20 '14 at 00:52
  • possible duplicate of [Unreported IOException? What is wrong with this code?](http://stackoverflow.com/questions/13922027/unreported-ioexception-what-is-wrong-with-this-code) –  Mar 20 '14 at 00:54
  • are you saying I need to possibly duplicate that code? or have I dupliacted that code? (which I have not btw lol) – user3424637 Mar 20 '14 at 00:56
  • Following the instructions the question I mentioned as a possible duplicate, you either need to throw a checked exception in a try block, or you need to specify that the method throws a checked exception. –  Mar 20 '14 at 00:58

2 Answers2

0

Either throw or catch "FileNotFoundException"

I have doubt are you not using any IDE like Eclipse or IntelliJ. It will throw a compilation warning there. Anyways this code should work

public void extractTokens() throws FileNotFoundException
    {
        // extracts tokens from the text file
        File text = new File("E:/LEWIS BC 2/project 1/ElectricToolData.txt");

        Scanner scanner = new Scanner(text);

        String toolName = scanner.next();
        String itemCode = scanner.next();
        String power = scanner.next();
        String timesBorrowed = scanner.next();
        String onLoan = scanner.next();
        String cost = scanner.next();
        String weight = scanner.next();

        // System.out.println(parts.get(1)); // "en"
    }
aviundefined
  • 802
  • 2
  • 10
  • 25
  • No I used to use eclipse, but now we have to use BlueJ for our java work, the code now compiles sucessfully, thank you much appreciated :) – user3424637 Mar 20 '14 at 01:09
0

You might encounter an IOException and/or FileNotFoundException so you should catch these 2:

public void extractTokens() throws IOException, FileNotFoundException{

    }
Abiel Paltao
  • 315
  • 2
  • 12