permaculture playing cards*
The moose likes Beginning Java and the fly likes PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PrintWriter" Watch "PrintWriter" New topic
Author

PrintWriter

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
PrintWriter pw = new PrintWriter(new FileWriter(new File("hai.txt")));
pw.print("ha");

i got the following compilation errors:

<identifier> expected
[javac] pw.print("hai");
[javac] symbol : class print
[javac] location: package pw
[javac] pw.print("hai");
[javac] ^
[javac] 2 errors
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
With only the code snippet you supplied, it's hard to say what the possible cause of the errors may be. It may be due to code not being placed inside a method. Try this code snippet and it should work fine.



Regards,
JD
[ March 01, 2005: Message edited by: John Dell'Oso ]
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
Hi John, thanks for your reply. One more doubt.
if i define PrintWriter pw = new PrintWriter(new FileWriter(new File("hai.txt"))); in the outer class,
then how can i access pw within inner class for pw.print("hai");?
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Kri,

If you declare your PrintWriter reference (pw in your case) inside a method, then you cannot access it from within an inner class. To access the PrintWriter from your inner class, it would have to be defined as either an instance or class variable of the outer class.

Regards,
JD
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
Hi John,
I declared pw as class variable of the outer class.
then how can i access pw within inner class for pw.print("hai");?
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Kri,

Try this out. Is this the sort of thing you are looking for. If not, then perhaps post your code.



Regards,
JD

[ March 01, 2005: Message edited by: John Dell'Oso ]
[ March 01, 2005: Message edited by: John Dell'Oso ]
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
HI John, My code looks like this. I don't have main method in both the class.

import java.io.*;

/** * @author John Dell'Oso * */
public class PWTest2 {

PrintWriter pw = new PrintWriter(new FileWriter("hai.txt"));

.....



class InnerPW {


// how can i get the pw object here ??
pw.print("ha");
pw.close();
}

}
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Kri,

If you remove the main method from the code in my previous post, it will still compile. I only put the main method in so that you could invoke it and see something happen (i.e. "ha" being written to the file).

When declaring your PrintWriter reference pw, you cannot initialise it outside of a method as you will be required to do something about the IOException due to the FileReader class in the initialiser. That's why I set it to null first, then I initialise it in the method within the inner class. That's why I declared the method printHAI() as throwing an IOException. I could have also put the method code inside a try-catch block, but I wanted to keep the code simple enough in the hope that it would resolve your problem.

Regards,
JD
[ March 01, 2005: Message edited by: John Dell'Oso ]
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Kri,

After my last post I realised that perhaps I should have posted the code for the "printer" class and another class that instantiates and invokes the "print" method of your "printer" class.

Try this - here's the "printer" class:



And here's a tester class for the above class:



Is this more what you are looking for? By the way, you probably don't really need to use inner classes here - unless of course you are learning about inner classes and how they work I suppose.

If this still does not help, please post ALL your code.

Regards,
JD
[ March 01, 2005: Message edited by: John Dell'Oso ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PrintWriter