• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Declaration Not Recognized  RSS feed

 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following lines of code are contained in the Main method of the same class in the order shown. Upon compiling, the last line generates the following error ... "Undefined error: foFileName". If the variable foFileName is replaced by a string in quotes ("test") in this line, the error does not occur. Shouldn't the last line recognize that the variable foFileName was previously declared as a String?

________________________________________________________
StringBuffer foFileNameBuffer = new StringBuffer(delRef);
foFileNameBuffer.append(".fo");

String foFileName = foFileNameBuffer.toString();

File foFile = new File (foFileName);

Writer foOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(foFileName),"UTF8"));

__________________________________________________________

Thanks.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the full code, because I was able to compile that code successfully?
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's too much code to post here - over a couple of hundred lines in the Main method, and it requires a number of JAR files to compile. I've therefore just included some of the try and catch blocks to put it more in context.

_________________________________________
try {
StringBuffer foFileNameBuffer = new StringBuffer(delRef);
foFileNameBuffer.append
(".fo");
String foFileName = foFileNameBuffer.toString();
File foFile = new File (foFileName);

// Create a file if it does not exist;
boolean success = foFile.createNewFile();
if (success) {
// File did not exist and was created.
} else {
// File already exists.
}
} catch (IOException e) {
}


try {
Writer foOut = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(foFileName),"UTF8"));
foOut.write(stringXML);
foOut.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
_________________________________

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable foFileName goes out of scope when the try block within which it is declared exits.

Please use UBB code tags when posting code to preserve its formatting.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's too much code to post here - over a couple of hundred lines in the Main method...
That sounds like a pretty long method. In general methods should have a single responsibility. If my methods go over 15 or so lines alarm bells start ringing in my head that there should probably be some refactoring going on soon. Perhaps you should think about breaking that main() method up into 10 or so smaller methods. Or if it seems sensible, a few small classes with shorter methods. That will probably end up making your life a lot easier when you go to debug your program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!