[Logo]
Forums Register Login
PrintWriter
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
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 ]
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");?
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
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");?
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 ]
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();
}

}
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 ]
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 ]
Are we home yet? Wait, did we forget the tiny ad?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 1552 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 19, 2018 20:26:32.