4

I am working with very large strings whose length range from 0 to 2*10^5. When I try to print the strings on the console or using command line via System.out.println, nothing shows up. Only strings/substrings with 4096 characters show up. Also, I get no errors.

I also tried to print the characters one at a time using System.out.print(chararray[i]) but to no avail.

I even tried tried to use the below but it did not work.

StringWriter stringWriter = new StringWriter();    
BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);    
bufferedWriter.write(str);   //does not display       
StringBuilder sb = new StringBuilder(str);    
System.out.println(sb);   //does not display

Displaying is important here since I am submitting my program on HackerRank and my program does not pass the test case due to this issue.

Can anyone please help?

Note: I know the strings are being stored properly since I was able to verify their lengths using str.length();

Community
  • 1
  • 1
Suraj Poojary
  • 57
  • 1
  • 6
  • 2
    what about sb.toString() ? – Stefan Dec 30 '15 at 19:06
  • 1
    Maybe this will fix it for eclipse http://stackoverflow.com/questions/8912202/character-limit-for-system-out-println-in-java – user3284549 Dec 30 '15 at 19:07
  • I am using IntelliJ and able to print string length of 8192 using StringBuilder. – YoungHobbit Dec 30 '15 at 19:11
  • No, the eclipse setting doesn't work too. I had tried that earlier and even now. sb.toString not working too. – Suraj Poojary Jan 13 '16 at 20:33
  • One thing thats pissed me off at least twice, sometimes Eclipse won't show a large string properly in the console, it's blank. Copy that blank line (it's not really blank) and put in NotePad or NP++, paste it, see if it has data. Twice now, eclipse was showing nothing but it was actually printing a value to the console - you just can't see it, WTF? – JGlass Jul 26 '22 at 15:03

2 Answers2

4

By default, Eclipse limits the amount of text in the console output window. So if you are running a program which produces a lot of output, the Console will only display the last n lines and you lose the rest.

Here is a tip which will allow you to have unlimited output in the console:

  • Go to Window > Preferences > Run/Debug > Console
  • Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)

Credits

frogatto
  • 28,539
  • 11
  • 83
  • 129
1

Try dividing the string into multiple chunks and then write to a text file. You can refer below link to divide the string : How to split a string into equal parts and store it in a string array

Mithun Khatri
  • 636
  • 3
  • 9
  • 22