Censoring emails in Java I'm working on a simple method that would censor emails (GPDR stuff) so they won't show the full name of a user, with 1st character, optional dot, 1st character after the dot and whatever is left after the @ to be left uncensored. So, the intent is like this:
Email to be censored: name.lastname@email.com
Desired effect: n***.l*******@email.com
I've written this:
public static String getCensoredUsername(String username) {
    if (username.contains(&quot;@&quot;)) {
        char[] chars = username.toCharArray();
        for (int i = 1; i &lt; chars.length; i++) {
            boolean isCurrentCharacterADot = chars[i] == '.';
            boolean isPreviousCharacterADot = chars[i - 1] == '.';

            if (chars[i] == '@') {
                break;
            }
            if (chars[i] == '.') {
                chars[i] = '.';
            } else if (!isCurrentCharacterADot &amp;&amp; !isPreviousCharacterADot) {
                chars[i] = '*';
            }
        }
        return String.valueOf(chars);
    }
    return username;
}

It feels to me very Java 6. What would make it look better?
There is nothing wrong about being &quot;too Java 6&quot;!
First few notes about naming and naming convention:

username is, in fact, the email, so should be email
getCensoredUsername for the same reason should be getCensoredEmail but in Java get, by convention, is used only for getter or when you're applying very little logic; thus is better to use an &quot;action-subject&quot; type of name like censorEmail
Being Java a type language, calling a variable chars is not helpful, that should be emailChars or even just email if that is not colliding with other variables, the compiler will tell you the type.

Biggest problem though, is the data validation, you should check at least that email is not null.
Normally you would check that the email is &quot;valid&quot; but if you take a look at the email syntax you might notice that's almost impossible to do it without writing a formal parser (or using some library). So let's say we want just censor the &quot;reasonably valid&quot; emails.
Your approach to go char by char in the username part is not very flexible: what if tomorrow you want to have f****o.f*****i@gmail.com? You need to add other flags, and wouldn't be very enjoyable.
I suggest you to start from the censor method and build bottom up from that.
Here is my solution
  public static String hideLastChars(String name) {
    Objects.requireNonNull(name, &quot;String should not be null&quot;);
    if (name.length() &lt;= 1) {
      return name;
    } else {
      return name.charAt(0) + IntStream.range(1, name.length()).mapToObj(i -&gt; &quot;*&quot;).collect(Collectors.joining());
    }
  }

Now that we have our &quot;logic&quot; we need to do a bit of input validation.
  public static String censorEmail(String email) {
    Objects.requireNonNull(email, &quot;Email should not be null&quot;);
    String[] splitEmail = email.split(&quot;@&quot;);
    if (splitEmail.length &gt; 1 &amp;&amp; splitEmail[0].length() &gt; 0) {
      return censorUsername(splitEmail[0]) + &quot;@&quot; + splitEmail[1];
    }
    throw new IllegalArgumentException(&quot;Please provide a valid email&quot;);
  }

Now we know that the email is not null, it does contain a '@' and user is there.
We just need to apply our hideLastChars to each piece.
We have a little problem here because if you just split on each . you will end up losing trailing dots, for example in an email like john.doe....@gmail.com.
A solution is to use Lookahead regexp to split on each . but keeping it in the split result, this is the regexp: ((?&lt;=\.)|(?=\.)), for example &quot;john.doe...&quot;.split(&quot;((?&lt;=\.)|(?=\.))&quot;) will return { &quot;john&quot;, &quot;.&quot;, &quot;doe&quot;, &quot;.&quot;, &quot;.&quot;, &quot;.&quot; }
Now we know how to map each piece and how to extract it.
  private static String censorUsername(String username) {
    return Arrays
        .stream(username.split(&quot;((?&lt;=\\.)|(?=\\.))&quot;))
        .map(scratch_1::hideLastChars)
        .collect(Collectors.joining(&quot;&quot;));
  }

I suggest you to create a unit test to test the logic so every time you find a new email that creates problem you can add in the test and see what happens (What about p.g@curry.com?).
I've run few examples using your code and mine. Here's the result




INPUT
ORIGINAL
REVIEWED




name.surname@gmail.com
n***.s******@gmail.com
n***.s******@gmail.com


name.surname.p99@gmail.com
n***.s******.p**@gmail.com
n***.s******.p**@gmail.com


name.surname.p99.@gmail.com
n***.s******.p**.@gmail.com
n***.s******.p**.@gmail.com


@gmail.com
@*****.c**
Exception: Please provide a valid email


user.com
user.com
Exception: Please provide a valid email


user%example.com@example.org
u***********.c**@example.org
u***********.c**@example.org


&quot;john..doe...&quot;@example.org
&quot;****..d**...&quot;@example.org
&quot;****..d**...&quot;@example.org


&quot; &quot;@example.org
&quot;**@example.org
&quot;**@example.org


null
Exception: Cannot invoke &quot;String.contains(java.lang.CharSequence)&quot; because &quot;username&quot; is null
Exception: Email should not be null


...p...g...@gmail.com
...p...g...@gmail.com
...p...g...@gmail.com


p.g@curryfication.com
p.g@curryfication.com
p.g@curryfication.com