fodon
fodon

Reputation: 4645

Strange behavior with java strings

I have two string variables ticker and detail. I'm trying to print out the two strings in one line. It just wouldn't work. I've tried so many different ways of doing this. To exclude the possibility of an uninitialized string I tried printing them out in different lines ... this works.

This example works ... except that the output needs to be in one line.

            System.out.println(ticker);
            System.out.println(detail);

And the output is:

IWM
|0#0.0|0#0.0|0#-4252#386|
GLD
|0#0.0|0#0.0|0#-4704#818|

When I try to put the output into one line in any of many ways, I get only the ticker ... the detail string is just not printed ... not to console or to file. Here are some example code snippets that produce the same result:

Attempt 1:

 System.out.println(ticker.concat(detail));

Attempt 2:

System.out.println(ticker+detail);

Attempt 3:

StringBuffer sb = new StringBuffer();
sb.append(ticker);
sb.append(detail);
System.out.print(sb.toString());

Attempt 4:

System.out.print(ticker);
System.out.println(detail);

In all the above attempts, I get the following output ... as if the detail part is ignored:

GOLD
BBL
SI

What could be causing these symptoms? Is there a way to get the two strings printed in one line?

Upvotes: 6

Views: 207

Answers (2)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

This might be better suited as a comment, but then I couldn't write the code snippet I need to write.

Where do the Strings come from? Are they from a file that might contain some odd control characters? If you're not creating the String yourself, you should examine them to look for embedded vertical carriage returns or other weirdness. Do something like this for both the detail and ticker Strings:

for (int i=0; i<detail.length(); ++i)
    System.out.println((int) detail.charAt(i));

and see if you get anything in the non-ASCII range.

Upvotes: 7

JuanZe
JuanZe

Reputation: 8157

May be the first string ends with a "\n", the newline (line feed) character ('\u000A')

Upvotes: 2

Related Questions