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??:mad:
Code :
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????:eek:
Can someone PLEASE tell me what is happening here???? :confused:
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 {}
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.
Re: Suggestion to declare variable that is declared
Quote:
Originally Posted by
Norm
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?? :confused:
Re: Suggestion to declare variable that is declared
Quote:
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 {
Re: Suggestion to declare variable that is declared
Quote:
Originally Posted by
zlloyd1
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
So, if I am reading what you are saying here correctly, I need to use (, instead of { after try?? :confused:
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.
Re: Suggestion to declare variable that is declared
Quote:
Originally Posted by
KevinWorkman
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!! @};-