2

So I've got a program that generates large binary sequences, and if the string length goes above 4094 it doesn't print. Here's a code snippet the highlights the problem:

private static void ALStringTest() {
        String al = "1";
        for (int i = 0; i < 5000; i++) {
            al += "1";

            System.out.println(al.length());
            System.out.println(al);
            System.out.println(al.isEmpty());
        }
}

What's interesting is the length continues to increase, and the boolean value stays false, but I'm unable to see the strings of length 4095 and above.

It's also not a printing error, as I've attempted to write the strings to xml and they don't appear either, all I get is spaces equal to the strings length.

Edit:

I've tried printing a file using this snippet and I have the same problem:

private static void ALStringTest() throws IOException {
        File fout = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(fout);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        String al = "1";
        for (int i = 0; i < 5000; i++) {
            al += "1";

            bw.write(al);
            bw.newLine();

            System.out.println(al.length());
            System.out.println(al);
            System.out.println(al.isEmpty());
        }
        bw.close();
    }

However, people have confirmed this works on external machines (thanks) (as well as on my own using javac, I'm lead to believe this may be Eclipse specific.

Anyone know why Eclipse might be doing this?

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
  • What "strings `4095`"? Nothing here would print anything like that. – chrylis -cautiouslyoptimistic- Mar 24 '16 at 04:36
  • 1
    Can you explain what you mean with "not print"? Maybe http://stackoverflow.com/questions/8912202/character-limit-for-system-out-println-in-java helps to solve your problem?! I think it's a IDE limitation. – Philipp Mar 24 '16 at 04:36
  • @chrylis strings of length 4095 and above, is what I mean. – Darkstarone Mar 24 '16 at 04:38
  • @Philipp that would be a reasonable assumption if when I tried writing to `xml`, it wrote correctly. However, as I've stated in the last paragraph, the `xml` element that contains the string is spaces equal to the string length. – Darkstarone Mar 24 '16 at 04:39
  • 1
    Try running your program using a terminal or a command-prompt using `javac` and `java`. I tried running your snippet and it prints all the strings up to length 5000 without any issues. As @Phillipp said, it might be an IDE limit. – rgamber Mar 24 '16 at 04:41
  • Can you show us how you write to XML. I just saw the `System.out`. Maybe that will explain why, because normally Java prints more :), I wrote enough XML outputs myself to know that there is no limitation around 4095 :). – Philipp Mar 24 '16 at 04:44
  • @rgamber thanks for confirming, it appears you and Philipp are correct - it's IDE specific. I'm not entirely sure how the XML is written, I'm using a rather large biology package (BEAST2). However, I also tested it with a `BufferedWriter` and I had the same problem. I'll post the snippet for that. – Darkstarone Mar 24 '16 at 04:49
  • As others said it may be IDE limitation, the snippet prints all 5000 Items without any Issue. – Narendran Solai Sridharan Mar 24 '16 at 04:44
  • `al == ""` is always false. Try `al.equals("")` or `al.isEmpty()` – Bohemian Mar 24 '16 at 04:51
  • `al.equals("")` also always returns false. As does `al.isEmpty()`, likely because the string is not empty, but spaces equal to its length. – Darkstarone Mar 24 '16 at 04:52
  • 1
    Just copy the output from the console of your IDE and paste it in notepad. You will see long series of 1s. Its the console of the IDE (eclipse) which I guess has some limit on the number of characters it can show. But if you do copy paste of the output on notepad you will see actual output. The blanks you see are actually long series of 1s which is why you are not able to get true for your equality condition – nits.kk Mar 24 '16 at 04:53
  • @nits.kk that works! Seems strange Eclipse won't show me strings in `xml` and `txt` files, but the copy and paste proved it was there. – Darkstarone Mar 24 '16 at 04:54
  • Darkstarone, `==` compares string references. To compare string values always use `.equals()` as @Bohemian pointed out. Also in this case it will always be `false` because you never assign it an empty value which you are checking against. – rgamber Mar 24 '16 at 04:54

3 Answers3

1

the boolean will stay false as hashcode of al is some value and that of "" is 0, == checks for reference.

shilpa
  • 17
  • 2
  • In Intellij, i was able to print the string value, which IDE you using?It can be IDE limit. I used IntelliJ – shilpa Mar 24 '16 at 04:49
  • al.equals("") also always returns false. As does al.isEmpty(), likely because the string is not empty, but spaces equal to it's length. I'm using Eclipse! – Darkstarone Mar 24 '16 at 04:53
1

So it turns out it was an IDE issue: enter image description here

Simply copying it to a text editor revealed the strings. I'll update when I find the offending option.

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
0

The possible reason of this maybe the defect of console which is rapidly printing output results. So that maybe it's only happening in console output. I've tested those each and the result was sometime it prints false only more than 6 times or sometime it prints only length That shouldn't be happened as scenario. But everything works fine when we use thread and make it sleep even 1 millisecond. The output is fine enough as codes,

class ThreadTest extends Thread {
    public ThreadTest() {
        super();
    }

    public void run() {
        String al = "1";
        for (int i = 0; i < 5000; i++) {
            try {
                sleep(1);
                al += "1";
                System.out.println(al.length());
                System.out.println(al);
                System.out.println(al.equals(""));
            } catch (InterruptedException e) {
            }
        }
    }
}

Call this in main method

new ThreadTest().start();
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68