6

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?

7
  • 1
    I don't see how the first output and the last output are related. They don't contain the same thing at all. You're probably not printing what you think you're printing. Try making a SSCCE (sscce.org). Commented Jan 12, 2012 at 13:07
  • yes, this is run in real time, so the outputs are slightly different, but the format is some ticker stored in ticker ... an some detail stored in detail variable. Commented Jan 12, 2012 at 13:08
  • 1
    Could you show us the actual content of the ticker and detail Strings? You could debug the code and get the text there... Commented Jan 12, 2012 at 13:10
  • 1
    What does it mean? Is there a fictional time? Commented Jan 12, 2012 at 13:11
  • The first printed statement matches what is shown in the debugger. Commented Jan 12, 2012 at 13:13

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

9 Comments

that is an interesting thought ... I'll run the test.
there are a bunch of 0s ... I guess the system doesn't delete the 0s and assumes that terminates the string. I'll work on that idea. Is there a standard library routine to get rid of trailing zeros?
ASCII NUL definitely counts as weirdness! If you can strip those off before creating the Java String objects, I bet your problem will go away.
You are using byte values for characters, it's not the system's fault if you put wrong values in it.
There is nothing wrong with having byte 0x00 in Java strings, so why would the constructor need to ignore them.
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.