• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing to a file...Please help...

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok you guys...I tried to do something different...I can read files great like this...but now I am trying to write to a file...
Here's what I have so far...
BUT I'm no where near done...I still need to ask a user if he wants to read from a file or write to one..duhhhhhhhhhhhh I have no idea what I'm doing. Any suggestions???
I get an error with this code...here's the error..

F:\Java II\PetRecordDemo1.java:58: 'catch' without 'try'
catch (IOException e)
^
1 error

Tool completed with exit code 1

I know it's telling me I need a 'try'...but why?? Duhhhhhh, on my part.





[ March 23, 2005: Message edited by: Rose Evans ]
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using just one catch block and in that block you can check for what kind of exception you've caught and proceed accordingly.

example psuedo code;

catch exception
if exception is IO do this
if exception is whatever do this
else do a generic error message
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the cause of the compiler error message, if this code snippet is accurate, is that you have omitted an ending brace in the for loop of your first catch block. So the compiler sees the second catch as occurring inside the first catch. See?

It's perfectly legitimate to list catch blocks in ascending order of the exception heirarchy. I've never seen multiple exceptions handled inside one catch block, and I know that had I done so in my last job my code never would have made it through the technical review board.
[ March 23, 2005: Message edited by: Jeff Bosch ]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like a misplaced curly bracket.

The for loop within catch(FileNotFoundException e) block is missing a curly bracket and then after the catch (IOException e) block there is an extra curly bracket.


ack. I really botched it when I tried inserting a code block. Maybe you'll get the idea without it.
[ March 23, 2005: Message edited by: Carol Enderlin ]
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carol -

Could you please reformat your code block? It's causing the topic to stretch way off to the right because there are no line breaks in your snippet.

[Nevermind! You beat me to it!]

Thanks!
[ March 23, 2005: Message edited by: Jeff Bosch ]
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK guys...I figured something out all by myself!!! Yeahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh!!!

Thanks for your help....I'm gonna see if I can make it do what else I want it to do!!!
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool! I believe that if you figure it out yourself, the knowledge gained will always be yours.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's perfectly legitimate to list catch blocks in ascending order of the exception heirarchy. I've never seen multiple exceptions handled inside one catch block, and I know that had I done so in my last job my code never would have made it through the technical review board.



Whoops sorry about the bad advice. I've seen something like this before

And thought I'd throw it out, but I'm happy to learn something new and thank you for the correction.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that it's bad advice, it's just something I've never seen before. Sometimes questions like this come down to the standards used by whatever shop the programmer works in.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK line breaks in my snippets? Now how is it I go about making my stuff not so long where it doesn't run off the page? Hahhahahaaa...I concentrate so hard on my studies....till I am just clueless to anything....beginning to think I'm brain dead for sure.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rose -

That was for Carol, not for you. Your code snippets are well-snippeted!
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

Oh ha ha...well I thought it might have been me who had the bad snippets..all of my code and stuff is running off the side of the page too, everyone's is..and I'm having to scroll around to read them. Ha.

Wonder why it's doing that?
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rose -

When you use the code tags, ultimatebb preserves the formatting. Part of that formatting is line-wrapping. If the line's not wrapped in your code, it won't wrap in the browser. I try to make sure my code doesn't go beyond column 60 to avoid the no-wrap problem. If my original code is wider than that, I'll find a way in my post to break the line into two. After all, Java, like C and C++, doesn't care a'tall about white space.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

Oh I see, well you surely taught me something today. Give yourself a pat on the back.
I'll try to remember that next time, so my stuff won't wrap. Ha.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No wrap music here, eh?

(Did I just hear a collective groan?)
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha ha....you're funny!!!
My Java programming is doing great!!!
I figured out something all by myself. Yeah,
patting self on back!!!
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Hentay -

Whoops sorry about the bad advice. I've seen something like this before





I spoke in haste. This type of exception handling is common when you have to determine the exception type from among multiple exceptions at the same level of the exception heirarchy. In such a case, you can catch the common superclass of the exception, then query the exception type.

Sorry for the confusion!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic