Help Needed With "Unique Characters in a String" Interview Question

Link to question:
https://www.codecademy.com/code-challenges/code-challenge-unique-characters-in-a-string-java

My code:

import java.util.*;

public class UniqueCharacters {
  public static void main(String[] args) {
    System.out.println(uniqueCharacters(""));
  }

  public static boolean uniqueCharacters(String strIn) {
    if (strIn.length() == 0) {
      System.out.println("Error");
      return true;
    }
    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < strIn.length(); i++) {
      if (list.contains("" + strIn.charAt(i))) {
        return false;
      }
      list.add("" + strIn.charAt(i));
    }
    return true;
  }
}

This consistently gives a result of 4/5 tests passed. I believe the issue is with this part of the question:

Note that if the function is called with an empty string (“”), the program should return an error message.

However, I don’t know how to fix this. If you try to change the method’s return type to something like Object so you can pass an error message or boolean as the result, the tester breaks. If you try to throw an exception, the tester breaks. I don’t know how else to pass an error message, and neither making the method return either true or false when the input is a blank string works. Any help is greatly appreciated!

1 Like

Same here. I also cannot find the solution threads like with the JavaScript coding challenges. My guess ist, the challenge is broken. The Method only can return true or false, because it’s returntype is boolean as in the template stated.

The only way to recive a Message for a false input is to throw an exception, as far as i know. I chose the IllegalArgumentException because it was for me the most fittig. But throwing an Exception breakes the tests completely.

So after all, I just repleated in my words what you already wrote. No solution. Sorry.

EDIT: I saw you found the solution. For everybody else, if you have an empty String, this is the way to exit:

System.exit(0);