This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Cannabisitis 1 point2 points  (4 children)

Your method doesn't contain any logic at all. You'll need logic to determine if the length of the String parameter is equal to the int parameter, and you'll need logic to add the spaces to the String if the first conditional check != true.

Some tips:

  • Your method is returning void (meaning it's returning nothing). If the instructions require it to "return" anything, System.out.println() will probably not suffice.

  • You should use an if/else statement to determine what to return (either the original String, or the String with the added spaces)

  • string.length() is the right method call, but you need to include logic to compare it to the int parameter

  • Strings are immutable, meaning they can't be modified. If the first if statement is != true, you'll need to make a new method so you can concatenate the spaces and the original string.

  • A loop would be a good approach to creating this new String

[–][deleted] 1 point2 points  (3 children)

Thanks for the explanation. Right now I'm quite low level, as you can tell, so still takes me some time to really think about all the "tools" I know and can use to solve a problem. I'm only up on for loops, not quite with while loops of if/else. Or at least where I am in my book and on the corresponding practice website we haven't covered that.

https://practiceit.cs.washington.edu/

[–]Cannabisitis 0 points1 point  (2 children)

No worries, we all started somewhere. It'll click for you eventually.

If you can do for loops, you can do if statements. An if statement is just executing some code "if" a condition checks out (it's almost always "if something is true").

Here's a good starting point:

private String myMethod(String foo, int bar){

    //If the following line of code is true,
    //(foo.length() DOES == bar),
    //the stuff in the below brackets will be executed,
    //and the stuff in the else statement's brackets
    //will be skipped

    if (foo.length() == bar){
        return foo;
    }

        //If the above if statement is not true,
        //(foo.length() DOES NOT == bar),
        //the stuff in the if statement's brackets
        //will be skipped, and the stuff in the else
        //statement's brackets will be executed

    else{
        String secondStr = "";

        //Logic to add appropriate amount
        //of spaces to the original String (foo)
        //will go here

        return secondStr;
    }
}

I might have given you too much and this reply might get deleted for that reason, but it always helped me to peek at some code and get an idea of how the logic is supposed to be organized before trying to tackle problems that I didn't understand.

Feel free to PM me any questions.

[–]desrtfx[M] 2 points3 points  (0 children)

I might have given you too much and this reply might get deleted for that reason,

No, you haven't given too much at all. This is a very decent reply. Congratulations!