• 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

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26690
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73243
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic