1

I want to view the value of a String variable at a breakpoint while developing in Java with Eclipse.

The String variable is displayed as [47, 110, 109, 107, 111], which makes sense since a string is really an array of ASCII characters.

However, I'd prefer to not have to convert from ASCII values to characters every time I examine a string. How can I have the debugger display the string as opposed to an array of ASCII values?

6
  • 2
    It should be there, right next to the variable name in "s. Commented Aug 21, 2012 at 18:20
  • Not for me. Eclipse just gives me (id=95) to the right of the variable name. Commented Aug 21, 2012 at 18:22
  • 3
    My Eclipse debugger has always shown the String value in double quotes next to the id. Are you using the Variable view in the Debug Perspective? If not what view and perspective? Also, whenever I click on the variable in the source code at a breakpoint, Eclipse pops up what looks like a tooltip with the value (when its running and stopped at the break point) Commented Aug 21, 2012 at 18:29
  • 2
    Are you confident that your variable is a String? Can you add the actual code for the variable? Commented Aug 21, 2012 at 18:42
  • A quick workaround would be to select the variable and press Ctrl + Shift + D, D Commented Aug 21, 2012 at 18:49

3 Answers 3

1

Try converting your array to a string with the correct encoding by creating a new insatnce of the string object:

String s = new String(bytes,"ASCII");
System.out.println(s);

Here is a short example to demonstrate:

    String example = "This is an example";
    byte[] bytes = example.getBytes();

    System.out.println("Text : " + example);
    System.out.println("Text [Byte Format] : " + bytes);
    System.out.println("Text [Byte Format] : " + bytes.toString());

    String s = null;
    try {
        s = new String(bytes, "ASCII");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Text from bytes: " + s);

output:

Text : This is an example

Text [Byte Format] : [B@187aeca

Text [Byte Format] : [B@187aeca

Text from bytes: This is an example

References:

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

3 Comments

Thank you, where do I type this into? The main control flow or is there a "console" window into which I can insert java snippets?
You should be able to evaluate an expression.
@DavidFaux depends on where you are outputting your byte array contents I guess? see edited post
1

If you are sure your variable is a String:

MyString

The the debug view should automatically display it as a string.

DebugView

Make sure your variable is indeed a String.

Comments

-1

i have been in that situation before. I assume the problem occured upon the eclipse version while you were debugging. After changing to older version of eclipse or IntelliJ (2018) and jdk 8 => The Problem solved.

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.