简体   繁体   中英

appending char to char array java

I'm trying to write a method that takes in and a returns a character array. I want to replace every space ' ' with 'h''i'. I can't use the string class or any other helper classes. I'm getting stuck when at "total+= ch;" since they are two different types.
What i want

char[] characterStr = {'w','h','a','t','s',' ','u','p'}
System.out.println(characterStr);
System.out.println(Space(characterStr));

whats up
whatshiup

my code so far:

public static char[] Space(char[] input){
    char[] total;

    for(char ch: input){
        if(ch==' '){
            ch = 'h'+'i';
            total += ch;
        }
        else{
            total += ch;
        }

    return total;

Your fundamental problem seems to be that you don't seem to understand how arrays work in Java.

In Java, arrays have a fixed length , and each location can hold only one of the declared data type. So for instance, if you have this char[] : [w, h, a, t, s, , u, p] , its length will permanently be fixed at 8 .

You can replace the character in a given location, but if you do that more than once, you will just overwrite the value.

    char[] characterStr = {'w','h','a','t','s',' ','u','p'};
    characterStr[5] = 'h'; // [w, h, a, t, s, h, u, p]
    characterStr[5] = 'i'; // [w, h, a, t, s, i, u, p]

Also, you cannot use += to try to append a value:

    char[] characterStr = {'w','h','a','t','s',' ','u','p'};
    characterStr += 'h'; // compile error! operator += is undefined for these arguments

You have to assign a value to a given location, using the arrayname[index] = value; syntax.

What you need to do (as mentioned in the comments), is first calculate the size of the array you are trying to create, by counting the number of spaces in the original array. You are already looping through and looking for spaces, so you just need to replace the code where you try to modify the array with a count, and create the output array based on that size:

    int size = input.length;
    for (char ch: input) {
        if (ch == ' ') {
            size += 1;
        }
    }
    char[] total = new char[size];

Also note that I didn't just declare total as a variable of type char[] , but I used the new keyword to actually create it. Like other objects, arrays in Java need to be explicitly created, or you will end up with a NullPointerException when you try to use it.

Once you have created your new array, you can loop through your original input array and copy the non-space characters over (or 'h' and 'i' when you see a space).

You won't want to use a for-each loop for this, since you'll need to manually keep track of the array indices. (Hint: the index into the input array and the index into the output array won't be the same once you see a space).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM