4

Below is a code I'm having issues with:

public class testOutput {

    public static void main(String[] args) throws Exception {

        int count = 50000;
        String s = "Vinjith";

        for(int i=0;i<count;i++) {
            System.out.print(s);  // change this to println(s); and it works! 
            System.out.flush();
        }
    }

}

I'm using Eclipse Galileo - jdk 1.6/ jre6.

  • I've set no limit on the console output.
  • I've also tried the same program with BufferedWriter: doesn't work
  • It works when the variable count = 584; not more than that.
  • I do not get any output when i use System.out.print(s); but when i use System.out.println(s); i get 50000 lines of the string 'Vinjith'.

Thanks.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
vj07
  • 41
  • 4
  • 3
    `System.out.println()` behaves similar to `System.out.print` with the only difference that will add a line break after printing. The usage of one or another is based on your needs (that will define if the output works or doesn't). – Luiggi Mendoza Mar 04 '13 at 15:04
  • 2
    You'll have to specify exactly *what* does not work. – Lukas Knuth Mar 04 '13 at 15:05
  • What happens if you use an `OutputStream` for `System.out`? – Giovani Guizzo Mar 04 '13 at 15:23
  • okie .. after some research - this is an existing bug - https://bugs.eclipse.org/bugs/show_bug.cgi?id=19850 .. If you need to print it - set the Fixed Width Console -> Maximum character width as 1000 or whatever ur Eclipse version supports.. – user1428716 Mar 04 '13 at 20:37

3 Answers3

2

This is because of having too much characters on the same line, where Eclipse does not support that on its console (You will see nothing printing on the console). Try the same code on the command line and it should work.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Eclipse says it can print a max of 1000000 characters(as found in Windows->preference->run->console). It can hardly print 5000 characters in a straight line. – vj07 Mar 04 '13 at 15:17
  • his limit reaches to 585 i.e count>585 .. eclipse does not print anything – user1428716 Mar 04 '13 at 15:35
1

This is because the length of the characters you're printing on Eclipse console is going beyond the limits.

Try this and see if it prints or not.

System.out.print(s);  // change this to println(s); and it works!
System.out.println();
System.out.flush();

Also, regarding the limit issue, just try this out. In the preferences - > run/debug -> console, there will an check box called Fixed Width Console. Its max limit is 1000. Try to make it 1000 and run your original code as below. You'll see that it prints some characters and for the rest, throws up an Internal Error.

System.out.print(s);  // change this to println(s); and it works!
System.out.flush();
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • @vj07 - If you notice carefully, each line after changing **Fixed Width Console** has 1000 characters exactly. – Rahul Mar 04 '13 at 15:24
0

Have you tried this:

    for(int i=0;i<count;i++) {
        System.out.print(s);  // change this to println(s); and it works! 
    }
    System.out.println("---done");
    System.out.flush();

What happens when you try values of count (100, 500, 1000, 2000, 10000, etc.)?

Please post output from when it works and what 'count' is.

I've looked into issues with flush() before and it comes down to how your OS internally handles buffers. Most JRE's only define the interface and then rely on the OS to implement the actual behavior and in some cases it gets weird. See my answer to a similiar question.

Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • print(s); and println(s); works fine for count up to 584. After which print(s); does not work. but println(s) works. Output for print(s): VinjithVinjithVinjith..... for println(s); is a new line between the string. – vj07 Mar 04 '13 at 15:18
  • Posted another version for you to try. – Kelly S. French Mar 04 '13 at 15:29