• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatter (writing to files)  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,ok so I'm learning from a book called Java how to program by Paul Deitek and in fig 6.30 he says explains how to use the class formatter with an example the only thing is the example won't work for me I am getting an error so I figured from reading the error message that I need a try and catch block,BUT in the example he does not put his Formatter file in a try and catch block and it works for him so how come I have to use a try and catch block for this??? and what does the try and catch block do to this?


Thanks
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't usually need to handle Exceptions from Formatter and Scanner because they are consumed. There are methods for getting the last Exception but nobody ever bothers using them.
Two questions about your Formatter:
  • 1: How are you creating the new file and does it actually exist on disc?
  • 2: How are you closing the Formatter?
  •  
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You my be getting an checked Exception from the File constructor.
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:No, you don't usually need to handle Exceptions from Formatter and Scanner because they are consumed. There are methods for getting the last Exception but nobody ever bothers using them.
    Two questions about your Formatter:
  • 1: How are you creating the new file and does it actually exist on disc?
  • 2: How are you closing the Formatter?


  • Yeah I'm so confused because the book's example is supposed to work but my error is when I'm creating a new Formatter object,I get a " unreported exception FileNotFoundException; must be caught or declared to be thrown.

    so confused :s

    heres my code

     
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are using Formatter to create a File object, which is probably hidden somewhere in the innards of Formatter. It might be as simple as… and line 2 calls the File constructor. I couldn't get rid of that Exception even with a file chooser. You can read about file choosers in the Java® Tutorials.This is what I tried and I still found myself with the exception to catch. What is the difference in Deitel?
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You are using Formatter to create a File object, which is probably hidden somewhere in the innards of Formatter. It might be as simple as… and line 2 calls the File constructor. I couldn't get rid of that Exception even with a file chooser. You can read about file choosers in the Java® Tutorials.This is what I tried and I still found myself with the exception to catch. What is the difference in Deitel?



    yeah I'm quite confused as why you would have to throw or declare an exception I wouldn't think it would have caused an exception I will writ the exact progrom from his book VV

     
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have introduced a few spelling errors, e.g. Close().
    Still requiring that Exception be handled.
     
    Bartender
    Posts: 1603
    232
    Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe your version of the book simply left out the exception handling. The example in this version of the book does catch the exception.
     
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I do not think that Formatter has changed the Exceptions if declares; you could verify that by checking the API documentation for Java5 6 7 and 8. You won't find Formatter before Java5
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I do not think that Formatter has changed the Exceptions if declares; you could verify that by checking the API documentation for Java5 6 7 and 8. You won't find Formatter before Java5


    Thanks Campbell =) sorry I couldn't come back to solving this problem earlier I had work on the weekend,how come you can cretae and use the Scanner object/class without throwing or declaring exceptions but with the formatter you can't?
     
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem is with the constructors taking a File reference; in case there are problems with the file, there might be a FileNotFoundException thrown. Neither the constructors of Scanner nor of Formatter will handle that particular Exception, but both pass it back to the user calling method to decide what to do about it.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!