import java.io.PrintStream;
import java.util.Scanner;
public class OutputTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintStream out = new PrintStream(System.out);
while(true)
{
int in = scan.nextInt();
if(in == 0) break;
System.out.println(in*in);
}
out.flush();
}
}
I'm expecting this program to take a list of numbers, one per line, and then when it hits a zero, print all of their squares, one per line. Instead, it's just echoing each number I type with the square.
Ideally, I'd actually include the line:
System.setOut(out);
and then write the rest of my program normally (except with the flush at the end), but this had the same result of echoing right away.
However, changing the PrintStream
to a PrintWriter
did make this work as expected, however I cannot do System.setOut
since PrintWriter
doesn't extend OutputStream
.
Why isn't this behaving as I expect?