java System.out.println() strange behavior long string
Asked Answered
S

5

6

Can somebody explain me why this code does not print the numbers?

      String text = new String("SomeString");
      for (int i=0; i<1500; i++) {
                text = text.concat(i+"");
      }
      System.out.println(text);

Result

      SomeString

If I lower the number of runs to 1000 it works, why?! And also if I add not only a number but also a character, it works.

Ok New Update:

Thanks for the code examples. I tried them all but what I found out is, that the console
actually display the numbers but only in fontcolor white. But the first part of the String SomeString is black.

I use jdk1.7.0_06 !

Siloa answered 30/8, 2012 at 12:57 Comment(5)
It prints fine for me. Are you sure you are running the code you think you are?Kierstenkieselguhr
does not work for me, version - java version "1.7.0_01"Falk
@PeterLawrey : which version are you using?Falk
Java 6 update 32 and Java 7 update 3.Kierstenkieselguhr
This is not a Java issue. Just the way you look to the result within Eclipse...Selassie
G
6

This is eclipse bug. Fixed width console fixes the output.

Getter answered 30/8, 2012 at 13:43 Comment(1)
worked for me, for others just in case: right-click in the console, preferences. It should be the first check box under console.Spineless
A
0

String.concat() accepts a String parameter.

If you add "a number and a character" you are adding a string because the + operator understands you are chaining String and numeric data.

Anyway code runs fine to me, numbers appended till 1499 as expected.

Asphodel answered 30/8, 2012 at 13:3 Comment(1)
This is why the OP is passing i+"" as parameters to concat().Shana
S
0

There are a couple things you could try. I'll give you an example of both.

First, in Java you can simply add strings together. Primitives such as int should be automatically converted:

  String text = new String("SomeString");

  for (int i = 0; i < 1500; i++) {
            text += i;
  }

  System.out.println(text);

Second, if the first method still isn't working for you then you can try to explicitly convert your int to a String like so:

  String text = new String("SomeString");

  for (int i = 0; i < 1500; i++) {
            text += Integer.toString(i);
  }

  System.out.println(text);
Sanity answered 30/8, 2012 at 13:3 Comment(0)
K
0

To do the same more efficiently

  StringBuilder text = new StringBuilder("SomeString");
  for (int i = 0; i < 1500; i++) {
        text.append(i);
  }
  System.out.println(text);

Both examples work for me on Java 6 update 32 and Java 7 update 3.

Kierstenkieselguhr answered 30/8, 2012 at 13:5 Comment(5)
Thanks for the correction. I just found out that he actually prints the numbers but in textcolor white. I have no idea what is going on.Siloa
exactly, it is in white color, what could be the reason? did you run on cmd prompt?Falk
When you try to select the text, you should be able to see it (if its white text on a white background)Kierstenkieselguhr
but why is the first part "SomeString" black and the numbers whiteSiloa
It shouldn't be. I can only imagine its a bug in eclipse.Kierstenkieselguhr
R
0

Woah, this is weird. I got the same result. At first glance, it looks like a bug in the JVM, but I tried running the program from the command-line and it works fine. It must be a bug in the Eclipse console. I found that changing the console to have a fixed width solves the display issue.

I also found that if you replace i + "" with i + "," it displays fine. It seems there's something Eclipse console doesn't like about having a long continuous stretch of pure numbers.

    String text = "SomeString";
    for (int i = 0; i < 15000; i++) {
        // text = text.concat(i + "");  // Doesn't display correctly
        // text += i;                   // Doesn't display correctly
        text = text.concat(i + ",");    // Displays correctly
        // text += i + ",";             // Displays correctly
    }
    System.out.println(text);

This bug is somewhat worrying to me. Good find!

UPDATE: I tried just printing a long line of "xxxxxx" and found that up to 32000 characters are displayed correctly. When the line goes to 32001 it's not displayed. When I put "12345" + "xxxxxxxxx...", I was still able to display 32000 "x" characters which means the line length is longer than 32000, so it's nothing to do with total line length. It seems that it's to do with the length of parts of String objects.

Rondelet answered 30/8, 2012 at 13:11 Comment(4)
The first variant: does it crash or is there no display at allSiloa
It doesn't crash. It displays SomeString. If it crashed I'd actually be much happier.Foretopmast
For me the numbers are displayed now, but thy are white. Can you copy&paste them out?Siloa
Yes. Copy and paste works, if I use Ctrl-A, Ctrl-C to copy all the console it pastes the full strings. But I'm not seeing anything written in white text. I can't select parts of the string.Foretopmast

© 2022 - 2024 — McMap. All rights reserved.