Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - General Questions (Q.No. 10)
10.
/* Missing Statement ? */
public class foo 
{
    public static void main(String[]args)throws Exception 
    {
        java.io.PrintWriter out = new java.io.PrintWriter(); 
        new java.io.OutputStreamWriter(System.out,true); 
        out.println("Hello"); 
    } 
}
What line of code should replace the missing statement to make this program compile?
No statement required.
import java.io.*;
include java.io.*;
import java.io.PrintWriter;
Answer: Option
Explanation:

The usual method for using/importing the java packages/classes is by using an import statement at the top of your code. However it is possible to explicitly import the specific class that you want to use as you use it which is shown in the code above. The disadvantage of this however is that every time you create a new object you will have to use the class path in the case "java.io" then the class name in the long run leading to a lot more typing.

Discussion:
10 comments Page 1 of 1.

Ala said:   1 decade ago
It doesn't compile for me in NetBeans 6.7.1. It says "cannot find symbol constructor PrintWriter()"

Oversteer said:   1 decade ago
This is totally wrong, there's no constructor for either PrintWriter or OutputStreamWriter and you also need System.out.println unless you have a static import in place.

Ankit337ce said:   1 decade ago
This code is not compile there are no suitable constructor found for PrintWriter().

Savita said:   1 decade ago
Yes all you are right. Same error coming in my eclipse.

Raja said:   1 decade ago
So, what is the correct answer?

Mac said:   1 decade ago
Hey all!

Its true that here no need to import package but written code is incorrect and there is many way to correct the code.

Amarjit said:   1 decade ago
Static import is needed because you are using out.println() without System.

Amarjit said:   1 decade ago
Static import is needed because you are using out.println() without System.

Enrique said:   10 years ago
The statement is incomplete. It should state

java.io.PrintWriter out = new java.io.PrintWriter(new java.io.OutputStreamWriter(System.out), true);

It is to state that the auto flush is on for the PrintWriter that is using as a stream of characters the standard output.

Sorry, I am writing with my mobile phone.

Shree said:   9 years ago
Is that code is correct? Anyone describe it in detail.

Post your comments here:

Your comments will be displayed after verification.