• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Simple file question

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



It has problem about this line:





 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because that variable is declared inside of the try block and because of that it's not visible from outside of the try block.
The solution: declare the variable outside of the try block and initialize it inside of it.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to guess that your compiler complains about the variable "inchar", because it is out of scope on that line. You declare it within the braces of your "try" block, so it is not visible within the braces of the "finally" block.

I have to guess because you have not provided us with an error message. In the future, I recommend that you give a little more information about what the problem really is -- if this had not been pretty obvious to me, I wouldn't have made a guess, and I wouldn't have searched hard. To get good help with questions, you need to provide folks with as much information as you can easily get, at least.

You might also try looking at the error message. One of the really good things about Java is the accuracy of its compiler's messages, i.e., it *usually* tells you exactly what is wrong and where.

rc
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Any suggestion to make it better?
 
lowercase baba
Posts: 12992
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:It has problem about this line:

Please read our FAQs on why ItDoesntWorkIsUseless and why you should TellTheDetails.

(you can click on those phrases to be taken directly to the specific FAQ).
 
Sheriff
Posts: 22257
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:Any suggestion to make it better?


How about a null check? What will happen if the FileInputStream cannot be created? In other words, if line 12 throws an exception?
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic