trouble with CharAt

600 views Asked by At

I am suppose to make a simple program that would take a users input, and put spaces between each single letter. So for example, user enters mall, and it returns M A L L(on same line). I am trying to make a loop with a if statement in it.But I think I would need CharAt for it, so if the string is greater value then 1, I would declare a variable to everysingle character in the string(that the userinput). Then I would say put spaces between each letter. I am in AP computer science A, and we are practicing loops.Everything underthis, is what I have done so far. And the directions are in the comment above code.And im useing eclipse,java.

/**
 * Splits the string str into individual characters: Small becomes S m a l l
 */
public static String split(String str) {
    for (int i = 0; str.length() > i; i++) {
        if (str.length() > 0) {
            char space = str.charAt();
        }
    }
    return str;
}   
4

There are 4 answers

5
Georgian On BEST ANSWER
  1. You don't modify method parameters, you make copies of them.
  2. You don't null-check/empty-check inside the loop, you do it first thing in the method.
  3. The standard in a for loop is i < size, not size > i... meh

    /**
     * Splits the string str into individual characters: Small becomes S m a l l
     */
    public static String split(final String str) 
    {
        String result = "";
    
        // If parameter is null or empty, return an empty string
        if (str == null || str.isEmpty())
            return result;
    
        // Go through the parameter's characters, and modify the result
        for (int i = 0; i < str.length(); i++) 
        {
            // The new result will be the previous result,
            // plus the current character at position i,
            // plus a white space.
            result = result + str.charAt(i) + " ";  
        }
    
        return result;
    }   
    


4. Go pro, use StringBuilder for the result, and static final constants for empty string and space character.

Peace!

2
fonZ On

Ask yourself a question, where is s coming from?

char space = s.charAt(); ??? s ???

A second question, character at?

public static String split(String str){
    for(int i = 0; i < str.length(); i++) {
        if (str.length() > 0) {
            char space = str.charAt(i)
        }
    }
    return str;
}
0
Siva Atta On

My solution uses concat to build the str2, and trim to remove last white space.

public static String split(String str) {
     String str2 = "";
     for(int i=0; i<str.length(); i++) {
        str2 = str2.concat(str.charAt(i)+" ");
     }
     return str2.trim();
}
0
Mansha Mannan Ul Haque On

@Babanfaraj, this a answer from a newbie like you!! The code is very easy. The corrected program is-

class fopl
{
    public static void main(String str) 
    {
    int n=str.length();
        for (int i = 0;i<n; i++) 
    {
        if (n>=0) 
        {
            String space = str.charAt(i)+" ";
            System.out.print(space);
        }
    }
}   
}

Happy to help you!