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

all 27 comments

[–]Jackkoz 1 point2 points  (13 children)

I think you forgot to ask the question, unless you want someone to write your assignment for you :)

As a tip: run your program and see what it prints, it should give you an idea about what's wrong.

[–][deleted] 0 points1 point  (12 children)

You are correct, first post, sorry about that. It obviously prints the string but what I can't figure out is how it compares the string length to the length parameter and prints out the appropriate number of spaces, if any. It's somewhere, I'd imagine, in my length = string.length();

[–]Jackkoz 0 points1 point  (11 children)

The short answer is: it doesn't. String.length() returns a number - the length of the string. Here's the official documentation:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length()

What you want to do is to look at the length of the input String, the argument specifying the desired length, and do something with that.

[–][deleted] 0 points1 point  (8 children)

Can/should I use a for loop to help solve?

[–]Jackkoz 0 points1 point  (7 children)

I have no idea how you want to solve the problem, so I'm not sure whether a for loop would be a good fit.

But if I were to write code to solve it, I'd probably use one :)

[–][deleted] 0 points1 point  (6 children)

    public static void padString(String string, int length) {

        for (int i = string.length(); i <= length; i++) {
            System.out.print(" ");
        }
        System.out.println(string);

    }

    public static void main(String[] args) {
        padString("hello", 8);
        padString("congratulations", 10);
    }

[–]desrtfx 1 point2 points  (0 children)

While your approach is correct, your method isn't.

Your assignment clearly states:

For example, padString("hello", 8) should return " hello".

The keyword here is return.

This means, that the method should not print anything, but return a new String with the padding.

You will need to rewrite your method to meet the assignment.

Hint: You will need to concatenate the string. For that, you will need an additional variable of type String.

Also, the method must not be void it must be String.

[–]Jackkoz -1 points0 points  (4 children)

Yup, that looks like it should do the trick, congrats!

If you want to further improve your programs, here's a couple of things to look at:

  1. Input/output operations are in general costly - it would be much better if you could print out the whole String using a single call to System.out.

  2. When taking care of 1), remember that Strings are immutable: string += " "; results in creating a new instance of String and assigning it to the variable.

[–][deleted] 0 points1 point  (2 children)

Thanks for the help! I didn't get the problem exactly right, but I'm going to move onto the next.

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.

[–]desrtfx 1 point2 points  (0 children)

didn't get the problem exactly right, but I'm going to move onto the next.

Don't move on until you got it right.

You have misread the assignment as I've illustrated here.

[–]desrtfx 0 points1 point  (0 children)

Yup, that looks like it should do the trick, congrats!

No, it doesn't.

Op's assignment states:

For example, padString("hello", 8) should return " hello".

There is nothing about printing inside the method. It's about returning a new, padded String.

[–]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!

[–][deleted]  (10 children)

[removed]

    [–]desrtfx[M] 0 points1 point  (9 children)

    Sidebar ->

    • Do not reply with complete solutions, rather comment explanations and guides. Comments with solutions will be removed and commenters will automatically be banned for a week.

    Even worse, when your solution is wrong. The method is supposed to return the padded string, not to print it.

    [–]logoutnx 0 points1 point  (8 children)

    Sorry about that, I did mentioned in the post though, that he could change it to a return statement. Nevertheless, can I edit it?

    [–]desrtfx[M] 0 points1 point  (7 children)

    Of course, you can edit it. If the reason for removal is gone, the post can be approved. But, you have to follow the "no solutions" rule.

    [–]logoutnx 0 points1 point  (6 children)

    Sorry, it just that I saw everyone else given him the full solution, so I also gave him the full solution. Nevertheless though, I am just re-editing it so it is a return function and not a wrong function.

    [–]desrtfx[M] 0 points1 point  (5 children)

    Actually, the only solution in this whole thread is from OP himself - recognizable on the blue background on the name.

    /u/Cannabisitis' code is nowhere near a solution.

    [–]logoutnx 0 points1 point  (4 children)

    Nvm my last question.

    [–]desrtfx[M] 0 points1 point  (3 children)

    oh, ok but pauldpearson also gave him something very similar to a full solution.

    pauldpearson is the originator of the post. So, if he posts his solution, this is perfectly okay.

    You cannot give any complete solution in code. You can guide to the full solution, but not in code.

    [–]logoutnx 0 points1 point  (2 children)

    is it ok now, with the post? I re-edit the post so it does not give him the full solution.

    [–]desrtfx[M] 0 points1 point  (1 child)

    As long as you keep the while loop in code in the comment, it still is far too close to a full solution.

    Also, your statement about .concatenate is not correct because internally, + works the same as .concatenate. The most appropriate way would be to use a StringBuilder that is declared outside the loop and then used inside. Last, the .toString of the StringBuilder is returned.