1

I'm doing an assignment which requires a method with a string and an int as parameters. The method should pad the parameter string with spaces until its length is the given length. For instance, padString("hello", 8 should return "hello___"(the _ represents the three spaces)) If the int is higher than the length of the string, then it would simply return the string. I'm having trouble getting the "padding" part of the program down.

As this assignment is early on in the book, I assume it can be done with beginner stuff such as forloops, parameters, and common string methods as I'm not supposed to use if/else statements yet.

Here's the—obviously flawed—code that I have currently:

public class Exercise11 {

public static final String word = "congratulations";
public static final int length = 10;

public static void main(String[] args) {
    padString();

}

public static String padString(String word, int length) {
    if (word.length() >= length) {
        return word.substring(0,length + 1);
    } else {
        String thing = word;
        return word + addSpaces(word, length);
    }

}

public static void addSpaces(String word, int length) {
    for (int i = 1; i <= length-word.length(); i++) {
            return (" ");
        }
}

}

By the way, is there a way to add something such as spaces onto a String variable with a for loop? Thanks for the help.

Jack L.
  • 385
  • 1
  • 11
  • 30

4 Answers4

3

That's a reasonable start...

padString is expecting two parameters, a String and int, so this...

public static void main(String[] args) {
    padString();
}

Could be changed to...

public static void main(String[] args) {
    padString(word, length);
}

The next problem is in you addSpaces method. The return statement in your loop means that the loop will only ever execute once and will only ever return a single space character.

Instead, you need to be concatenating the space on each loop to a temporary String, which you will pass back, for example...

public static String addSpaces(String word, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length - word.length(); i++) {
        sb.append(" ");
    }
    return sb.toString();
}

So, if I run...

System.out.println("[" + padString("hello", length) + "]");

I get

[hello     ]
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
1

The immediate problem is that your for loop in addSpaces is immediately returning a single space, no matter how many spaces you need to add to it. A better approach would be for addSpaces to return String and to add spaces to an internal String variable until it was the correct length, then return that.

Also, in Java, it's customary to start loops from zero unless there's a particularly strong reason to start somewhere else. In your case, word.length() might be a good starting point, but 1 isn't.

chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140
0

Your add spaces returning nothing.It's void.And internal logic also not fulfilling your requirements.

Your addSpaces method should something like

public static String addSpaces(String word, int length) {
     String spaces="";
    for (int i = 1; i <= length-word.length(); i++) {
           spaces +=  " ";
        }
     return  spaces;
} 

And prefer to use StringBuilder to append instead String for better performance.

With StringBuilder

 public static String addSpaces(String word, int length) {
         StringBuilder spaces=new StringBuilder();
        for (int i = 1; i <= length-word.length(); i++) {
               StringBuilder.append(" ");
            }
         return  spaces.toString();
    } 

Then you return word + addSpaces(word, length); works perfect.Assuming i did'nt miss anything else :).

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
0

Thanks guys, it finally works now.

Here's the final copy of the code:

public class Exercise11 {

public static final String WORD = "congratulations";
public static final int LENGTH = 10;

public static void main(String[] args) {
    System.out.println(padString(WORD, LENGTH));
}

private static String padString(String word, int length) {
    if (word.length() >= length) {
        return word.substring(0,word.length());
    } else {
        return word + addSpaces(word, length);
    }

}

public static String addSpaces(String word, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length - word.length(); i++) {
        sb.append("_"); // these are more visible than empty spaces
    }
    return sb.toString();
}

}

I still have a couple question however. What is the point of constructing the "Stringbuilder" and what is the "append" method?

Because the book doesn't have these things at this point, is there another method?

Jack L.
  • 385
  • 1
  • 11
  • 30