• Post Reply Bookmark Topic Watch Topic
  • New Topic

The art and science of java: Example of PrintWriter using FileWriter as parameter  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am looking at chapter 12 of the art and science of java intro text and I see the following example code demonstrating how to write a new file:



(I added the imports at top myself.) However, I am getting compiler errors for the lines: , which says that the constructor PrintWriter( FileWriter ) is undefined, and
, which says close() method is undefined. Pretty sure the only real problem is that PrintWriter is not accepting the FileWriter as constructor, but I don't see why. I have tried this on machine with JRE 1.4 and it worked as expected, creating new file titled "hello.txt", prints line of "Hello world!" in that file, and saves it to the directory I picked in the dialog. But I can't get it to work on this machine that uses Compiler 1.6, Java Runtime v8u25.

I have also tried using just a string in the parameter for PrintWriter, such as , and from what I can tell by reading the java spec for java 8, this should work. http://docs.oracle.com/javase/8/docs/api/index.html. But I get error message for that constructor as well.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of those imports, the ones you added yourself, are being used to identify PrintWriter and FileWriter? Because if you look at the standard Java API, the constructors you used do in fact exist. That suggests to me that PrintWriter is coming from some nonstandard API -- your "acm" imports would be the obvious culprits.
 
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You've called your class PrintWriter.
The compiler grabs the first class it comes to, in this case the one it's compiling, when looking for a class referred to in code.

Change the name and try and avoid using a class name from the more common bits of the JDK.
 
Dennis Ouyang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the below code worked! :



I was using the acm packages since that is how the book and the class that uses it (Stanford CS106) chooses to teach intro java. I used the acm.program.* library again and it was not the problem, I just needed to rename my class to something other than printWriter. Very good thing to know to avoid further mistakes like this, embarassed to admit I spent a good day or so trying to fix a bunch of other stuff that was not broken. Thanks Dave Tolls!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Java7/8? If so you should use try with resources to open your writer, and you don't need to close it specifically.
I am surprised the import with PrintWriter.* didn't cause problems. Remove the .* from it.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Ouyang wrote:I was using the acm packages since that is how the book and the class that uses it (Stanford CS106) chooses to teach intro java. I used the acm.program.* library again and it was not the problem, I just needed to rename my class to something other than printWriter. Very good thing to know to avoid further mistakes like this, embarassed to admit I spent a good day or so trying to fix a bunch of other stuff that was not broken. Thanks Dave Tolls!


No problem.
You're not alone on this, by the way.
I've seen this a lot, presumably because these names are obvious ones to use (which is why the JDK has used them). You just need to be a bit more descriptive, even is just PrintWriterTest...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!