-2

I am trying to print a really long character String using StringBuilder in Java. For ex. this code i'm using:-

 repeat1=new StringBuilder();
 for(j=0;j<long_string;j++)
   repeat1.append('5');
 System.out.print(repeat1);

The number of characters I am trying to print is more than (long_string=)80,000 or so. But the output gives a Runtime Error and the value that gets printed is Truncated after printing 65536 characters which I assume is the max +ve range of Characters in Java. Output is something like that :-

555555555555555555555555555555 ................................ ................................ ................................ ................................ 555555555555555555555555{-truncated-}

I don't know what I am doing wrong that it is not printing all the characters!!

Aseem Yadav
  • 728
  • 7
  • 15
  • 1
    why not simply split the string up into multiple chunks? –  Oct 06 '15 at 11:57
  • 4
    _What_ Runtime Error? I mean _which message_? – Thomas Oct 06 '15 at 11:57
  • you've got your answer, `65536` may be the max size that can be printed by the console for a single string – Hacketo Oct 06 '15 at 11:58
  • If you only want to print '5' why not create a loop that continously prints '5'? – Distjubo Oct 06 '15 at 11:59
  • I assume it's a test and he counts every 5 individually ;) – Thomas Oct 06 '15 at 12:00
  • I'm intrigued *not* to find anything about `String`'s max capcity [in the String section of the JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3.3) or in [the JavaDoc](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html). I *must* be missing it. That said, I'm sure it's **well** more than 80k. Probably somewhere nearer to `Integer.MAX_VALUE`. – T.J. Crowder Oct 06 '15 at 12:00
  • You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.Source: http://stackoverflow.com/questions/1179983/how-many-characters-can-a-java-string-have – hic1086 Oct 06 '15 at 12:03
  • 1
    @hic1086: Famously, [*An Array of Characters Is Not a String*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.9). We know that Oracle's JDK implements `String` *using* one, but that's an implementation detail. We also know that `String#length` is of type `int` and that `String` has `toCharArray` which returns an array. All of which is merely ***suggestive***. I'm really surprised the JLS doesn't say something definitive. – T.J. Crowder Oct 06 '15 at 12:05
  • 2
    Could you _please_ post the "Runtime Error" you are getting and the message it outputs with its stack trace? – daiscog Oct 06 '15 at 12:06
  • @T.J. Crowder : look at this ansewr: http://stackoverflow.com/questions/12120002/is-there-any-limit-for-string-size-in-a-java-program. – hic1086 Oct 06 '15 at 12:16
  • 1
    @hic1086: Again: We can *infer* all we like, and that's all Peter's done in that answer. Inference is the enemy of good engineering, however. I'll just repeat myself a third time: I'm surprised there isn't a clear statement of this in the JLS. I'm sure strings can have up to `Integer.MAX_VALUE` lengths. I'm just surprised we have to *infer* that when it can and should be *specified*. – T.J. Crowder Oct 06 '15 at 12:17
  • thank you all for downvoting , cudn't expect anything else as a new comer here..But Anyways !!! @Thomas I was getting the **Runtime error** inside an online IDE [link](http://ideone.com/SDnwrO) and when I was running it in **eclipse** , it gets terminated abnormally. – Aseem Yadav Oct 07 '15 at 06:22
  • @Distjubo even after using a loop to print the **string** , the program doesn't print characters more than 65536. Here is the link to prove that I have tried that as well [see here](http://ideone.com/SDnwrO) – Aseem Yadav Oct 07 '15 at 06:25
  • @t-j-crowder I surely would have posted the exact **runtime-error** I was getting, but as I was using an Online IDE (ideone), it just printed **65536** characters and at the top of the output it only showed **runtime-error** . You can see the whole program with output [here](http://ideone.com/SDnwrO) – Aseem Yadav Oct 07 '15 at 06:36
  • 2
    @AseemYadav `thank you all for downvoting , cudn't expect anything else as a new comer here.` ... please think about your attitude. It's true that if someone downvotes he should give an explanation or refer to one but that unfortunately isn't the case very often so you'll have to deal with that. I assume (since I didn't downvote) that the reason was that your question implied there is more information on the error that you didn't share and maybe the test case which doesn't make that much sense, i.e. it is not clear what you need that for. That's just a guess though. – Thomas Oct 07 '15 at 08:06
  • 2
    @AseemYadav `... when I was running it in eclipse , it gets terminated abnormally.` - what happens when you run it outside an IDE, i.e. directly from the command line? – Thomas Oct 07 '15 at 08:07
  • @Thomas I am sorry for that...But I did not have much information about the error. And, I am practicing some data structures questions (from a competitive programming website). I am looking for the solution for this problem because I solved the logic but not able to print the actual output. – Aseem Yadav Oct 07 '15 at 09:13
  • Hmm, in that case I'd say either try your solution at the site and see whether it works or look for how they want you to do output. As has already been stated it's normally not a problem of Java or structures used but of the output target. If it is just for testing (to have a look at the output yourself) try to write to a file and read the file. – Thomas Oct 07 '15 at 09:51
  • @Thomas yes its working when I directly run it from command line – Aseem Yadav Oct 07 '15 at 09:55

1 Answers1

4

You can create and print much larger Strings in Java.

To me it looks like your console/shell/terminal truncates the extremely long line.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Then why the runtime exception? – Maroun Oct 06 '15 at 12:00
  • 3
    Could conceivably be an I/O exception thrown by the I/O subsystem (caused by whatever System.out is connected to here on the "other end"). But it would help to see the actual exception. – Thilo Oct 06 '15 at 12:02
  • That's what I thought, +1. – Maroun Oct 06 '15 at 12:02
  • @thilo the runtime-error I was getting was inside the online IDE I was running this program on and when I tried running it on my local machine (inside Eclipse) , then the program terminates abnormally. Here is the link to the program [see here](http://ideone.com/SDnwrO) with the output and its error. – Aseem Yadav Oct 07 '15 at 06:30
  • 1
    That program works fine (I guess...) for me. Type "1 65537" into it and it prints a lot of output. No errors. – Thilo Oct 07 '15 at 08:13