Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Suggestion to declare variable that is declared

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Suggestion to declare variable that is declared

    The following simple clock of code won't work, and the reason I am getting is that it has reached the end of file while parsing, but if I add another }, it comes back saying it is unnecessary??
    package filez;
    import java.io.*;
    public class Filez{
    public static void main(String[] args){
    File f = new File("test.txt");
    try(
    FileWriter fw = new FileWriter(f); 
    fw.write("write this to file"); //Keeps saying identifier expected????
    fw.close();}catch(Exception ex){}
    }// This is the final closing bracket, but it is saying I reached end of file while parsing????
    This code is an exact duplication of code that a friend of mine wrote that works without a hitch, so I don't get this.
    What is wrong with the line, fw.write("write this to file");??
    I mean I declared fw in the previous line, FileWriter fw = new FileWriter(f);, so I don't see the issue here????
    Can someone PLEASE tell me what is happening here????


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Suggestion to declare variable that is declared

    How many opening brackets do you have? How many closing brackets do you have?

    I highly suggest you use proper formatting. Use indentation to indicate what's going on, and follow the proper conventions. For example, put each closing bracket on its own line, indented to the same level as the line that contains the opening bracket. Indent everything between two brackets an extra tab, and it'll be really obvious when something is missing.

    Also, don't confuse parenthesis () with curly brackets {}
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Suggestion to declare variable that is declared

    What version of the JDK are you using? There is a new version of the try statement in 1.7: try-with-resources
    It uses a ( instead of a { after the try.

    BTW The posted code is poorly formatted making it hard to read and understand. Nested statements need to be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face Re: Suggestion to declare variable that is declared

    Quote Originally Posted by Norm View Post
    What version of the JDK are you using? There is a new version of the try statement in 1.7: try-with-resources
    It uses a ( instead of a { after the try.

    BTW The posted code is poorly formatted making it hard to read and understand. Nested statements need to be indented.
    I apologize for the lazy formatting of my code, but I am writing this in notepad, and it is tiresome to keep indenting everything properly sometimes.
    So, if I am reading what you are saying here correctly, I need to use (, instead of { after try??

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Suggestion to declare variable that is declared

    use (, instead of { after try
    Depends on if you are using version 1.7 which has a new syntax. Before 1.7 it was always a {
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Suggestion to declare variable that is declared

    Quote Originally Posted by zlloyd1 View Post
    I apologize for the lazy formatting of my code, but I am writing this in notepad, and it is tiresome to keep indenting everything properly sometimes.
    Your entire problem is due to improper formatting. Please take the few minutes to properly format your code. It will make your life easier, and perhaps more importantly, it will make it easier for us to help you.

    Quote Originally Posted by zlloyd1 View Post
    So, if I am reading what you are saying here correctly, I need to use (, instead of { after try??
    Please read this: The try-with-resources Statement (The Java™ Tutorials > Essential Classes > Exceptions)

    You are using syntax that is halfway between a normal try block and a try-with-resources block, and we don't know which one you're actually trying to do.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Suggestion to declare variable that is declared

    Quote Originally Posted by KevinWorkman View Post
    Your entire problem is due to improper formatting. Please take the few minutes to properly format your code. It will make your life easier, and perhaps more importantly, it will make it easier for us to help you.



    Please read this: The try-with-resources Statement (The Java™ Tutorials > Essential Classes > Exceptions)

    You are using syntax that is halfway between a normal try block and a try-with-resources block, and we don't know which one you're actually trying to do.
    I am actually not truly adept at using exception handling in my code, so you could be exactly correct that I have misused the try catch block. Also, I write relatively small, simple programs generally, so indentation has not really been an issue for me, that is until I get a strange error that I do not agree with. So, I will go ahead and concentrate on proper indentation for even the smallest programs from here forward. Thanks for the advice, and the suggestions!!

Similar Threads

  1. Declared variables not carrying over
    By evanscott815 in forum AWT / Java Swing
    Replies: 6
    Last Post: October 4th, 2012, 11:52 AM
  2. How to declare byte enum
    By alex007 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2012, 05:51 AM
  3. Replies: 9
    Last Post: December 8th, 2011, 07:11 AM
  4. caught or declare might be thrown error
    By IDK12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2011, 01:55 PM
  5. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM

Tags for this Thread