dangerChihuahua007
dangerChihuahua007

Reputation: 20915

How do I view Java strings in the Eclipse debugger?

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?

Upvotes: 1

Views: 1420

Answers (3)

pham dat
pham dat

Reputation: 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.

Upvotes: -1

Snowy Coder Girl
Snowy Coder Girl

Reputation: 5518

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.

Upvotes: 1

David Kroukamp
David Kroukamp

Reputation: 36423

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:

Upvotes: 1

Related Questions