0

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

is the question I'm trying to solve. I can't change characters to be empty strings. I know the code is messy, but I'm trying to find out how to make the characters empty strings without using null character, Unicode, or min_value, etc. How do I do it?

public String startOz(String str) {
  char a = ' ';
  char b = ' ';
  String c = "";
  String d ="";
  if(str.length() <= 1){
    if(str.length() == 0){
      return "";
    }
    else
      if(str.charAt(0) == 'o'){
        return str;
      }
      else;
        return "";

  }
  else{

    if(str.charAt(0) == 'o'){
      a = 'o';
      c = Character.toString(a);
    }
    else;
      c = Character.toString(a);
      c = c.substring(1);
    if(str.charAt(1) == 'z'){
      b = 'z';
      d = Character.toString(b);
    }
    else;
      d = Character.toString(b);
      d = d.substring(1);

    c = Character.toString(a);
    d = Character.toString(b);
    return c + d;
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    Don't try to replace anything. Think of it as building a new string from scratch. – Sören Jun 30 '23 at 14:58
  • 1
    Also I recommend using better variable names, using `a` `b` `c` and `d` is a nightmare for anyone trying to figure out the code, including yourself. – Nexevis Jun 30 '23 at 15:00
  • You are setting your variables `String c` and `String d` redundantly many times. Setting a value only to immediately discard it and setting it to something else makes little sense. Having empty else blocks like `else;` also isn't really doing much except bloating your code. Removing some of the unnecessary code your code already becomes much more compact. And if you just get rid if the pointless char variables and instead directly work from building up an empty String you'll also get the result you want: https://ideone.com/skH6BO – OH GOD SPIDERS Jun 30 '23 at 15:10
  • I'm not sure if I fully understand the objective, but would it be met with `str = str.replaceAll("^oz.*", "oz");`? (Not that I'm saying you should do it like that) – g00se Jun 30 '23 at 15:28
  • @g00se no. `startOz("open")` should return `"o"` and `startOz("azimuth")` should return `"z"`. That is, if I understand OP correctly. – Sören Jun 30 '23 at 15:37
  • 1
    `return ((str.length() > 0 && str.charAt(0) == 'o') ? "o" : "") + ((str.length() > 1 && str.charAt(1) == 'z') ? "z" : "");`? – Andy Turner Jun 30 '23 at 15:39
  • Related: [Why is String immutable in Java?](https://stackoverflow.com/questions/22397861/why-is-string-immutable-in-java) – Peter Mortensen Jul 01 '23 at 08:42

2 Answers2

0

To achieve the desired behavior without using empty strings, null characters, Unicode characters, or minimum values, you can modify your code as follows:

public String startOz(String str) {
  String result = "";

  if (str.length() >= 1 && str.charAt(0) == 'o') {
    result += str.charAt(0);
  }

  if (str.length() >= 2 && str.charAt(1) == 'z') {
    result += str.charAt(1);
  }

  return result;
}
S.N
  • 2,157
  • 3
  • 29
  • 78
0

"... I'm trying to find out how to make the characters empty strings ..."

You can't, you'll need to use a String value.

You are implying the following.  Which is a syntax error.

char a = '';

It's because the char is a primitive data-type, thus it requires a value.

Whenever I run into this situation, I switch to a String value.

Reilas
  • 3,297
  • 2
  • 4
  • 17