Question: Encounter exception test issues I'm very new to Java. The assignment is about extracting the first and last several characters from a string to a

Encounter exception test issues

I'm very new to Java. The assignment is about extracting the first and last several characters from a string to a new one. My solution passes most tests. But I encountered issues when dealing with exception test.

Below is my code for solution.

Encounter exception test issues I'm very new to Java. The assignment is

Below is three exception tests I cannot pass.

about extracting the first and last several characters from a string to

I'm wondering how should I modify my code. If you think my code can be more simpler, you're welcome to comment as well. Thank you.

public static String firstAndlastCharacters (String text, int count) { // TODO: Please implement the method. // text.length()) { throw new IllegalArgumentException(); } StringBuilder result = new StringBuilder(); result.append(text, start: e, count).append(text.substring(text.length()-count)); return result.toString(); // --end--> } @Test void should_throw_if_input_string_is_null() { final IllegalArgumentException exception = assertThrows IllegalArgumentException.class, () -> StringHelper.firstAndLastCharacters( text: null, assertEquals( expected: "The text cannot be null.", exception.getMessage()); } count: 0)); @Test void should_throw_if_count_is_negative() { final IllegalArgumentException exception = assertThrows IllegalArgumentException.class, () -> StringHelper.firstAndLastCharacters( text: "Hello", count: -1)); assertEquals( expected: "Invalid count.", exception.getMessage()); } @Test void should_throw_if_count_is_greater_than_text_length() { final IllegalArgumentException exception = assertThrows IllegalArgumentException.class, () -> StringHelper.firstAndlastCharacters( text: "Hello", count: 100)); assertEquals( expected: "Invalid count.", exception.getMessage()); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!