0

I am working on a practice question:

Given a string, return a string made of the first 2 chars (if present), however, include the first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".

startOz("ozymandias") → "oz"
startOz("bzoo") → "z"
startOz("oxx") → "o"

Below is my solution, but I don't know why it shows "incompatible types: char cannot be converted to java.lang.String". I was wondering if anyone can help me with my code. Thank you so much!

public String startOz(String str) {
  if (str.length()==1 && str.charAt(0)=='o'){
    return 'o';
  }
  if (str.length()<1){
    return "";
  }
  if (str.length()>=2 && str.charAt(0) =='o' && str.charAt(1) !='z'){
    return 'o';
  }
  if (str.length()>=2 && str.charAt(0) !='o' && str.charAt(1) =='z'){
    return 'z';
  }
  if (str.length()>=2 && str.charAt(0) =='o' && str.charAt(1) =='z'){
    return "oz";
  } else {
    return "";
  }
}
3
  • 4
    Because 'o' is a char. "o" would be a string. Commented Jan 6, 2022 at 18:35
  • 2
    'o' is a char - a primitive type. It cannot be converted directly to the string "o" (notice the double quotes) - you'd need to explicitly do String.valueOf('o'). Commented Jan 6, 2022 at 18:35
  • 1
    just change the type of quotes from '' to "" for all return excpression Commented Jan 6, 2022 at 18:45

3 Answers 3

4

When you type a string in single quotation marks, it tells the compiler to consider whatever follows as a single character. Double quotation marks indicate a string. The function cannot return a char when it is expecting a String.

public String startOz(String str) {
  if (str.length()==1 && str.charAt(0)=='o'){
    return "o";
  }
  if (str.length()<1){
    return "";
  }
  if (str.length()>=2 && str.charAt(0) =='o' && str.charAt(1) !='z'){
    return "o";
  }
  if (str.length()>=2 && str.charAt(0) !='o' && str.charAt(1) =='z'){
    return "z";
  }
  if (str.length()>=2 && str.charAt(0) =='o' && str.charAt(1) =='z'){
    return "oz";
  } else {
    return "";
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!!
1

Rainmaker et al are correct: the problem is that your method should return a String (a Java object), not a char (a Java primitive). One good solution to the compile error is to return "o" (double quotes).

But perhaps you might find this a simpler solution:

public static boolean isNullOrEmpty(String s) {
  return s == null || s.isEmpty();
}

public String startOz(String str) {
  if (isNullOrEmpty(str)) {
    return "";
  } else if (str.startsWith("oz")) {
    return "oz";
  } else if (str.charAt(0) == 'o') {
    return "o";
  } else if (str.length() > 1 && str.charAt(1) == 'z') {
    return "z";
  } else {
    return "";
  }

4 Comments

Wow...thank you so much! I did not know "startsWith" and the solution you provided is so clear and really helps me think differently.
This runs, but if str.length()==0, startOz("") → "" it will show StringIndexOutOfBoundsException: String index out of range: 0
Correct. Or you'll get an NPE if it's null. I added an additional static method, equivalent to .Net string.IsNullOrEmpty().
You get java.lang.StringIndexOutOfBoundsException for input startOz("x")
0

You cannot retreive a char instead of Stirng. Do use String.valueOf(ch) instead.

By the way, just be a simple man! You can do the same with much less code.

public static String startOz(String str) {
    String res = "";

    if (str != null) {
        if (str.length() > 0 && str.charAt(0) == 'o')
            res += 'o';
        if (str.length() > 1 && str.charAt(1) == 'z')
            res += 'z';
    }

    return res;
}

Answer to comment.

  1. StringBuilder is a correct way to build a String, because String is immutable in Java. In case you are a beginner, I removed it.
  2. <condition> ? <true> : <false> is called a ternary operand. if is similar like if(<condition>) <true> else <false>;
  3. And finally I used trim() to remove \0 from the beginning and the end of the string. A also removed it.

You can see my updated result.

2 Comments

Thank you so much for your help! I am new to programming, and I was wondering if you could explain a little more. What's StringBuilder? I also don't understand the part "'o' ? 'o' : '\0'". Why do we need trim() at the end? Thank you so much!
@user13966876 see my answers above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.