From the course: Learning Java 11

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Find the bug in string truncation

Solution: Find the bug in string truncation - Java Tutorial

From the course: Learning Java 11

Solution: Find the bug in string truncation

- [Instructor] Let's find the bug in the chunky function. In the code, we access the string and run the substring method on it. The substring we create runs from index zero to what this math dot max expression returns. It will return either the length of the string or the max length value given in the input. Let's see what the code outputs when we run it as is. We'll change show expected result to true, so we can see what our code is supposed to return. With this execution, the input string was this is a sample string and the max length was three. This means the string returned should only be THI or a string with the characters from index zero to index two in order. THI is what appears in the expected results, but our code returned the full input string. It didn't truncate it. Let's consider how our code was evaluated given our input values. In this case, S dot length would've been 24 because there are 24 characters in the string. The max length was passed in as three. This means that…

Contents