简体   繁体   中英

Why is my expected string not matching the output?

I wrote a method (using BlueJ) that returns a user's bank account information as a single line of description:

public String toString()
    {
        return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
    }

And I wrote a unit test for the method that looks like this:

public void testToString()
    {
        Account account1 = new Account("Aurora", 772340912, 1000.00f);
        assertEquals("Test account toString failed.", "772340912    Aurora    $1,000.00", account1.toString());
    }

I've looked inside the "Resources" folder in BlueJ and it says: bluej.editor.tabsize=4 so I assume that this means a tab is equal to 4 spaces. However, even though I've used four spaces in the expected output, my unit test still fails. Any ideas? Thank you in advance!

A tab is not four spaces. bluej.editor.tabsize=4 means that the BlueJ system is rendering the tabs as if they were four spaces.

However, no matter how they are rendered, tabs are still tabs, something completely different from a space. '\\t' == 11 , while ' ' == 32 , and " " can't even be compared to an int since it's a String , not a char .

For the expected String, use "\\t" instead of " " .

A tab is a different ASCII character from the space character. This may likely be causing some sort of mismatch where the ASCII code for a tab is compared to 4 ASCII codes for space, which will not be equal.

So simply edit one or the other so that they match up!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM