1

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;
10
  • 2
    I think it is easiest if you construct a String from the array, replace all the spaces with "hi", then call toCharArray to get the resulting char array. Commented Feb 5, 2015 at 18:43
  • 1
    If you can't use String, then why did you declare your method as returning one? Commented Feb 5, 2015 at 18:44
  • 2
    It helps when you specify constraints in your question. Also, your method's return type is String. Commented Feb 5, 2015 at 18:45
  • 1
    Alright if you are returning a char[] you are going to have to make a new array with the appropriate size (+1 size for each space) and then add the values into the new array. I would use a regular for loop not a foreach Commented Feb 5, 2015 at 18:47
  • 1
    You cannot dynamically expand the size of an array as you seem to expect when you use +=. When you create total its size will be fixed, so you need to figure out how big it will be. You're replacing one character with two, so this isn't trivial but it should still be pretty easy. Hint: you'll have to iterate through input twice. Commented Feb 5, 2015 at 18:47

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't size start out as input.length instead of 0?
@chancea Oops, yes you're right. Originally I was counting the spaces and forgot to change that when I re-wrote it. Fixed now.

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.