Log Levels in Java Exercise

Hello, I am working on the Log Levels exercise. My intention is to return the string message. I used the substring method to locate this.


public class LogLevels {
    
    
    public static String message(String logLine) {
        throw new UnsupportedOperationException("Please implement the (static) LogLevels.message() method");
        return logLine.substring(11);
    }
    

    public static String logLevel(String logLine) {
        throw new UnsupportedOperationException("Please implement the (static) LogLevels.logLevel() method");
    }

    public static String reformat(String logLine) {
        throw new UnsupportedOperationException("Please implement the (static) LogLevels.reformat() method");
    }
}

I am getting this error

./src/main/java/LogLevels.java:6: error: unreachable statement
        return logLine.substring(11);
        ^

My goal was to find the start of the whitespace in the string, I used that as a marker to identify the beginning of the message. I then moved one index over to the first character of the message, which was at 11. With this idea, I was hoping to extract the message from the string, using the substring method. By not passing a second argument to substring, it should extra to the end of the string.

I did some research, one question I had was, how could I test this code without a main method for the entry point, I learned that some classes do not need main methods. I assumed we are using this code as a Utility class, to process long messages.


throw new UnsupportedOperationException("Please implement the (static) LogLevels.message() method");

I also tried my best research in order to learn this code, from what I learned, this code will throw this message if the method does not execute correctly?

I assumed the boilerplate code was the LogLevels.message() method, and it needed a simple way of extracting the message portion from the logLine string (being passed in as a parameter). I thought if I return that string (the parameter logLine), and call the substring method with it, at index 11 until the end of the string, that it should extract the message.

Am I processing this correctly? Or do I need to go in another direction entirely?

Thank you team! :smiley:

UnsupportedOperationException indicates a requested operation isn’t supported. The message informs us why it’s not supported, and that’s because we need to implement the method. At the moment, there’s nothing in the method body except the exception, and the exception will be thrown every time because it’s reached before we exit the method. You need to implement the method by adding your code to the body and then remove the line containing the UnsupportedOperationException.

You run the test suites following the track’s testing instructions. The test suite will import your code and go through a series of tests with particular inputs and expected outputs. Conceptually, you’re writing more of a library that gets imported by a program to do something rather than making a whole program from scratch. The main entry point is through the test suite as a result.

1 Like

The code will throw unconditionally. It’s true, the return statement cannot be reached.

Once you start to implement the method, you should remove that throw.

2 Likes

Thank you so much @BNAndras and @glennj for helping me understand. I feel so stupid! Hahaha! Of course, I have to remove the exception. I am not used this approach, but I find it to be very useful and creative of you guys! My approach to solving these exercises were similar to HackerRank, but I see it’s different. The concept you gave me @BNAndras really helps (the library concept). Thank you guys!

1 Like

You may find Exercism’s general explanation about the workflow with Test Driven Development helpful, too.

1 Like