-2

I have a relatively simple console output for debug purposes as I'm trying to figure out an error not connected to this issue. My code is the following:

System.out.println("info: " + element + " || " + getIDforName(element));

The variable element of type String is taken from a cmd output and might me empty/null. I would therefore expect it to output something like info: null || null. However it does not do that but outputs this: || null which is really puzzling my. I'd unterstand if it wouldn't write it completely for some reason or if it would write info: || but the actual output makes no sense to me as it can't just skip a part.

So my question is: Why does this happen and how do I fix it? If any additional code is needed I'll try to provide it.

EDIT: getIDforName(String name) is a method which compares some Strings and returns a String if there is a matching one.

Niklas
  • 375
  • 1
  • 3
  • 17
  • 1
    What is `element` and give the code for `getIDforName(element)`! – Am_I_Helpful Jan 26 '16 at 13:17
  • You should verify element before println -> see if it has a value, is null etc, and make an output. Otherwise, when it is not empty, null, display other info. Basic if else ... did you tried with another separator other than || ? – ares777 Jan 26 '16 at 13:17
  • 1
    Could it be that `element` stringifies to something that includes newlines? Is there an `info: ` on a previous line? – Biffen Jan 26 '16 at 13:18
  • use the debug to see the content – JMR Jan 26 '16 at 13:18
  • there is no "info: " on a previous line – Niklas Jan 26 '16 at 13:19
  • as to checking if `element` is empty: it doesnt matter. it should output null or nothing at all. But it shouldn't NOT output the `"info: "` String – Niklas Jan 26 '16 at 13:20
  • Please check what `element` really contains, for example by using `System.out.println(Arrays.toString(element.getBytes()));`. – Tom Jan 26 '16 at 13:27
  • 1
    *"But it shouldn't NOT output the "info: " String"* It isn't Javas fault. What the console _really_ prints depends on the console itself. For example `element` contains `\r` then some console ignore that and other, like the Windows console, will move the cursor to the beginning of the line and overwrite "info: " with " || null". – Tom Jan 26 '16 at 13:30
  • `System.out.println(Arrays.toString(element.getBytes()));` produces exactly what should be in the variable. So for Input `Logon` I get `[76, 111, 103, 111, 110]` – Niklas Jan 26 '16 at 13:30
  • And does Input *"Logon"* produce the "weird" output you've mentioned? – Tom Jan 26 '16 at 13:32
  • so I get `element` from `someJList.getSelectedValue().toString();`. Every time the very last element is the selected Value, this issue appears. So it does not depend on the value itself but the position in the JList – Niklas Jan 26 '16 at 13:36
  • And the last item is for example "Logon"? I currently assume that your `element` String only contains `\r` when the problem occurs. So can you please try to create a [mcve]? With a screenshot/copy from the console output? – Tom Jan 26 '16 at 13:43

4 Answers4

1

I checked both the content of element during runtime using the debugger which gave me a value of null and printed the byteArray values as pointed out by Tom which returned the correct value as it should have been (the bytes of a String for example [76, 111, 103, 111, 110] for input Logon.

So out of pure curiosity I used the trim() method of the String contained in element and somehow it works now. Even though none of the other tests yielded a result indicating there would be whitespaces.

UPDATE:

The reason why I had this issue was the following: The inital value fo the variable came from Windows cmd and was therefore separated by \r\n but I only split it by \n so on the last Item of the JList it just reverted the string written beore it wrote the \r to the console. Using System.lineSeparator() for splitting solved the problem at the cause instead of using trim() to cure the symptoms. A big thanks to Tom for pointing this out.

Niklas
  • 375
  • 1
  • 3
  • 17
  • 1
    Well, I wouldn't stop here. You have a problem somewhere in your code, so `element` gets an unexpected value. So you should try to find the source for the problem, instead of hiding the problem by using `trim()`. But it is up to you, what you want to do. – Tom Jan 26 '16 at 14:46
  • I totally agree but since this thread gave me no applicable answers so far I'll postpone solving this problem until I have enough time to think it over. – Niklas Jan 26 '16 at 14:47
  • 2
    Let me guess, you're working on Windows? Then the problem is quite clear :P. Windows uses `\r\n` as line separators. When you split by using `\n`, then `\r` remains on each String in the array and `\r` will result in the problem you have. Use `System.lineSeparator()` to split on. Or `\\s+`. – Tom Jan 26 '16 at 15:08
  • I do. But I was already aware of the newline issue after the end of the command so I already removed it or at least thought so.... (If that's what you're hinting on) – Niklas Jan 26 '16 at 15:09
  • *"If that's what you're hinting on"* Yes I was. I've edited the comment to provide a possible fix. – Tom Jan 26 '16 at 15:11
  • thanks a lot. still I'm kinda confused that the byteArray conversion didn't tell me there was a `\r` – Niklas Jan 26 '16 at 15:13
  • Has this suggestion solved your problem, then please update your answer accordingly, so future readers know that the problem was a remaining `\r` :). – Tom Jan 26 '16 at 15:17
  • will do so. Seems like the problem was all me... and some part of java not showing me the whole thing :D – Niklas Jan 26 '16 at 15:18
  • also it still seems strange to me that the other values were working perfectly even though they all must have had a `\r` remaining... – Niklas Jan 26 '16 at 15:23
  • Yes, that is strange that only the last element seemed to have this problem. How do you enter the text into the console and how do you read it? Do you write it line by line like: "abc" "123" "blub" ? And how do you read it? – Tom Jan 26 '16 at 15:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101692/discussion-between-niklas-and-tom). – Niklas Jan 26 '16 at 15:27
  • 1
    It doesn't look like I can use the chat using the mobile app :(. But anyway, it id possible that the inner lines uses the Unix line endings, whereas the last line uses Windows style for some reason. Since you read the output from another app, that app my writes hardcoded \n, but then you press return to acknowledge some operation, you would use the first \r\n. So splitting on \\s+ would be safer, when you have to deal with different line ending styles. – Tom Jan 26 '16 at 15:45
  • I just checked and you're right. using lineSeparator instead of \n everywhere doesn't work. – Niklas Jan 26 '16 at 15:50
  • Actually no. I get some arrayindexoutofboundsexception because of my code. Will need to look into it at some other time – Niklas Jan 26 '16 at 16:22
0

The easiest thing you can do is to verify the content of the element first, e.g. test if it's really null. It most likely isn't. E.g.

System.out.println("info: " + "\b\b\b\b\b\b" + " || " + null);

may produce

|| null

so I would suspect element to contain "\b\b\b\b\b\b" or any other control characters, as others suggested.

charlie
  • 1,478
  • 10
  • 20
  • You might want to write "may produces", instead of "produces", because depends on the console, how it treats control characters. The IDEA console for example just ignores them. – Tom Jan 26 '16 at 13:35
  • I might add I am using the IDEA console. IntelliJ IDEA CE to be exact – Niklas Jan 26 '16 at 13:39
  • 1
    @Niklas Your problem occurs with the following code: `System.out.println("info: " + "\r" + " || " + null);`. So as discussed in the comments of your question, please check that you String doesn't contain `\r` and try to create a [mcve]. – Tom Jan 26 '16 at 13:46
  • 1
    I just tried it with the debugger. element is `null` not `\r`. I'm working on the MCV example – Niklas Jan 26 '16 at 13:48
  • couldn't reproduce it in the MCV. however trim() solved the problem even though the debugger said the variable was `null` and the byteArray had the correct Values for the letters of the actual value... – Niklas Jan 26 '16 at 14:36
0

Probably this behavior is due to an empty String "" not a null one.

Try this :

System.out.println("info: " + (element.length() == 0 ? null : element) + " || " + element);

Test for null before passing element in System.out

Wael Sakhri
  • 397
  • 1
  • 8
  • So en empty `element` String is the cause for *"info: "* being missing in the output (according to OP)? – Tom Jan 26 '16 at 13:31
  • You'll get a `NullPointerException` if you try this code with `element` equal to `null`. – charlie Jan 26 '16 at 13:35
  • According the OP It's probably a bug .. trying to print something within the element maybe change something ! there is nothing predictable here – Wael Sakhri Jan 26 '16 at 13:36
  • But the OP wants to get `info: null || null`. So instead your code should be: `(null != element && element.isEmpty() ? null : element)`. – charlie Jan 26 '16 at 13:39
  • According to OP what you menionned is true ! about the last parameter `element` (a typo in my code) – Wael Sakhri Jan 26 '16 at 13:45
-1

A simple way its to override "toString()" method at your element class.

Check this thread:

Community
  • 1
  • 1
kunpapa
  • 367
  • 1
  • 9
  • element already is a String. my question doesn't concern the value of `element` but why `"info :"` is not being put out by the console. – Niklas Jan 26 '16 at 13:21
  • Oh I see, didnt understand it. I tryed your code in a java test and it seems working good. Output is correct, could you give us more info? – kunpapa Jan 26 '16 at 13:25