1

I am retrieving a player-list from a server and I am doing stuff with it (un-related to problem) later on. These un-related processes only work if the username doesn't have spaces, but instead has the character '_' instead of ' '. I am using this code to loop through each playername's character and then detect if its a ' ' and, if it is, replace it with a '_' but for some reason it doesn't get detected at all?

    char[] name;

    for(String playerName : chatlist) {
        name = playerName.toCharArray();

        for(int i =0; i < name.length; i++) {
            if(name[i] == ' ') {
                name[i] = '_';
            }
        }

        String nameS = new String(name);
        System.out.println(playerName + " -> " + nameS);
    }

An example of a username and output is the following:

Testuser 1 would output Testuser_1

ExampleUser One would output ExampleUser_One

Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91

5 Answers5

7

Use replaceAll("\\s+", "_"). It's the better way of doing it. Else you will be replacing each space with an "_" (is this what you want?)

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
3

Use playerName.replace(' ', '_')

Stuart Caie
  • 2,803
  • 14
  • 15
  • Why would this work any better? The OP's code works for me. – Duncan Jones Apr 22 '14 at 11:00
  • @Duncan - smaller / simpler code :) – TheLostMind Apr 22 '14 at 11:01
  • @WhoAmI This is not http://codegolf.stackexchange.com. That being said, the answer would be ok if it also addressed the underlying problem. Trouble is - there doesn't seem to be one. – Duncan Jones Apr 22 '14 at 11:02
  • @Duncan - I agree. Yet, the answer shows another way of doing it. – TheLostMind Apr 22 '14 at 11:06
  • 3
    It makes the programmer's intentions succinct and clear. "this is the player name with spaces converted to underscores", as opposed to 20 lines of code which, after someone reads through it and works out what it does, discovers it replaces spaces with underscores. – Stuart Caie Apr 22 '14 at 11:06
  • @StuartCaie I'm just pointing out that an answer should address the issue described in the problem. Maybe this would have been a good comment. Either way, I don't feel overly strongly (hence lack of down-vote). – Duncan Jones Apr 22 '14 at 11:08
2

This code works as expected:

private static List<String> chatlist = Arrays.asList("Mr. Who", "Dr. Me");

public static void main(String[] args) {
    for(String playerName : chatlist) {
        char[] name = playerName.toCharArray();
        for (int i = 0; i < name.length; i++) {
            if (name[i] == ' ') name[i] = '_';
        }
        String nameS = new String(name);
        System.out.println(playerName + " -> " + nameS);
    }
}

OUTPUT:

Mr. Who -> Mr._Who
Dr. Me -> Dr._Me

Maybe the space character you expect isn't a space at all? You may inspect the characters of the player names by printing out their codes like this System.out.println((int) ' '). The space character code is 32.

Harmlezz
  • 7,972
  • 27
  • 35
0

Your loop works for me. Are you sure chatlist isn't null?

But as others have said, a simpler way would be `name.replace(' ', '_'). This is better because it makes your code shorter and more readable. Everyone, including you, knows exactly what you're trying to do in that one line of code.

DonyorM
  • 1,760
  • 1
  • 20
  • 34
0

You could take a look at this answer and use String.replaceAll instead of a manual char replacement.

Just for clarification - in this answer a complete set of all whitespace characters is matched, instead of just space.

Community
  • 1
  • 1
minime
  • 344
  • 3
  • 10