5

I have been trying to solve an algorithm problem called Chebyshev's Theorem(https://www.acmicpc.net/problem/4948)

And I got an interesting situation. I haven't figured out the difference between yet. And I hope I can find the answer here.

And this is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Solution {
    public static void main(String[] args)  throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        while(true){
            System.out.println("start!!");
            int input = Integer.parseInt(br.readLine());        
            if(input == 0){
                break;
            }
            int ddable = 2*input;
            int answer = 0;
            ArrayList<Integer> base = new ArrayList<>();

            if(input == 1){
                answer = 1;
            }else { 
                System.out.println("It is not 1");
                for(int i = 2 ; i <= ddable ; i++ ){
                    base.add(i);
                }

                for ( int i = 2 ; i <= ddable ; i++ ){
                    for ( int j = 2 ; i*j <=ddable ; j++){
                        if(base.contains(new Integer(i*j))){
                            base.remove(new Integer(i*j));
                            System.out.println(i*j+"removed");
                        }
                    }
                }
                int count = 0;
                for ( int i = input ; i <= ddable ; i++){
                    if(base.contains(new Integer(i))){
                        count++;
                    }
                }
                answer = count;
            }
            System.out.println("syso: "+answer);
            bw.write("bw: "+answer);
        }
        bw.flush();
        br.close();
        bw.close();
    }
}

And this is my result:

enter image description here

As you can see, this just shows the result of 'System.out.prinln()'.

What's the reason?

c-an
  • 3,543
  • 5
  • 35
  • 82
  • 1
    From you screen you did not go out of the while loop, so flush isn't called so no print – azro Jun 25 '18 at 12:39
  • `BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));` with this line you have manipulated your console output into a different object. But the overall operation doesn't change. – soufrk Jun 25 '18 at 12:39
  • The entire point of using a buffered stream is to retain the text/data/bytes in memory and only push it out as needed to improve efficiency by reducing the number of system calls. – Peter Lawrey Jun 25 '18 at 12:41

2 Answers2

7

See the doc:

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

You can call flush or close to make sure the content be printed.

xingbin
  • 27,410
  • 9
  • 53
  • 103
4

This was a simple problem.

To print something out with 'BufferedWriter', you need to call 'flush()' after in the right time you want to print it out. So, in the code.

You need to fix this part.

    System.out.println("syso: "+answer);
    bw.write("bw: "+answer);
}
bw.flush();

to

    System.out.println("syso: "+answer);
    bw.write("bw: "+answer);
    bw.flush();
}

Then, It prints the result that you want and even if the loop runs, it prints fine without any problem.

And check this for 'flush()' : What is the purpose of flush() in Java streams?

It says

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

So, to be written out, you need to call 'flush()' after 'write()'. And 'bw' is still reusable if it hasn't closed('close()') yet.

c-an
  • 3,543
  • 5
  • 35
  • 82