Search code examples
javabluej

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=4so 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!


Solution

  • 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 " ".