0

I can make this code work if I modify the main method class, but I am supposed to make it work WITHOUT modifying it at all. I'm confused as to what is causing the error.

Here is the method I have written in a class

   public boolean addSongs(String fileName)throws FileNotFoundException
   {   
      Scanner scan = null;
        
      try //open the try block for FNFE
      { 
      
         //open the file, declare scanner, set endFile to false
         scan = new Scanner(new File(fileName));
         boolean endFile = false;
         
         
         
         while(!endFile) //continues until the end of the file 
         {
            try //try block for mismatch exception or no such element
            {
            
                //scan in line and read through it with new delimiter
               String line = scan.nextLine();
               Scanner read = new Scanner(line);
               read.useDelimiter("/");
               
               //scan in name, minutes, and seconds on the scanned line
               String scannedName = read.next();           
               int scannedMin     = read.nextInt();
               int scannedSec     = read.nextInt();
               
               
               //print the results
               System.out.print("Name:    " + scannedName + "\n" +
                                "Minutes: " + scannedMin + "\n" +
                                "Seconds: " + scannedSec + 
                                "\n-----------------------------------\n");
            
             
             //add to the playlist if no errors
               title.add(new Song(scannedName, scannedMin, scannedSec));
            
            }
            
            catch(InputMismatchException e)  // handle when data is N/A
            {
               String error = scan.nextLine();  //consume the error and return to see where error is      occurring
               System.out.print( " ERROR (cause): " + error + "\n");
              
            }
            
            catch(NoSuchElementException e) // handle when no more data in file
            {
               endFile = true;  //nothing in file set endFile
            }
            
         } //close try block
                  
         //close and return true
         scan.close();
         return true;
         
      }// close loop
            
      //return false if no file found
      catch (FileNotFoundException e)
      {
        System.out.println("THERE IS NO FILE BRETHREN");
         return false;
      }
   
      
   }// close method

Here is the code thats causing an issue in the main class

if (b.addSongs("revolver.txt"))
         System.out.println("Songs in file revolver.txt added successfully");
      System.out.printf("The playlist \"%s\" has a total playing time of %5.2f minutes\n", b.getName(), b.getPlayingTime());
       System.out.printf("It includes the song \"%s\" with a playing time of %4.2f minutes\n", song, b.getPlayingTime(song));
      System.out.printf("The longest song is \"%s\"\n", b.longestSong());
      if (b.removeSong("Taxman"))
         System.out.println("The song \"Taxman\" was removed successfully"); 
      System.out.println("\n" + b); }

This is the error message:

PLTest.java:32: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
 b.addSongs("revolver.txt");
           ^

My solution is to add in a FileNotFoundException throw in the main class, but as I stated before ._. I am banned from changing it. I don't see what keeps going wrong since the invoked method throws the exception. It's the only error that occurs and when I add the throw to the main class everything works perfectly.

For a side note: I attempted to put the throw in another method and got a duplicate error that went away when I just removed the throw. I don't know if that is relevant but my next guess is to find a way to change the boolean values without a try/catch?

  • It's not throwing an exception, `addSongs` is marked as been able to throw a `FileNotFoundException` which the caller is not dealing with. The "simple" solution would be to remove `throws FileNotFoundException` from the method defination, but, I would argue that it's really the callers responsibility to deal with the error, so I'd remove the `try-catch` in `addSongs` and wrap it around the callers workflow – MadProgrammer Feb 19 '23 at 05:22
  • I wish I could but my assignment specifically keeps me from altering the caller. I hate when I have to work with code I didn't write – ConfusedFetus Feb 19 '23 at 22:21
  • Well, either you need to remove `throws FileNotFoundException` or catch the exception at the caller level - those are your two options – MadProgrammer Feb 19 '23 at 22:27
  • i think i'm supposed to remove it. however I no longer know how to return a true/false for the boolean method. It's supposed to be false if the file doesnt open so i figured the way to do that was to throw the FileNotFoundException. Is there another way to test if the file opens properly that I'm missing? – ConfusedFetus Feb 21 '23 at 01:00
  • In the `catch` block. I'm pretty sure if you've made it to that point, the file couldn't be opened, otherwise, you're good – MadProgrammer Feb 21 '23 at 01:01
  • oh. my. gosh. i didn't realize that I could have a try-catch without specifying the throw in the method header. I thought it was a requirement. thank you so much, you are a lifesaver. – ConfusedFetus Feb 21 '23 at 03:52

1 Answers1

-1

Use

scan = new Scanner(new FileReader(new File(fileName)));

instead of

scan = new Scanner(new File(fileName));

and make sure that the text files are along side of the src folder. Hove the exceptions will solved.

  • 1
    how is this expected to avoid the compilation error "*unreported exception FileNotFoundException; must be caught or declared to be thrown*" ? (it does not change the signature of `addSongs()`, nor does it handle the exception) – user16320675 Feb 19 '23 at 09:30
  • The problem isn't with the `Scanner`, they are already handling the `FileNotFoundException`, the problem is with the method declaring that it can t throw a `FileNotFoundException` and the calling code not handling it. – MadProgrammer Feb 19 '23 at 22:33