0

I was making a new code where my objective was to make whatever key you press make my little "truck" go forward. I was trying to do this by adding spaces to my string, which was my truck.

String truck = "<o><o>-<o>~|#|¬";

    truck = new StringBuffer(truck).insert(0, " ").toString();

    System.out.println(truck);

I would like to know how to do a "loop" on this, where it add spaces with the quantity of keys that you pressed. Thanks for reading, have a nice day.

4 Answers 4

2

You could do this in a number of ways, for example...

You could attack the problem head on, for example...

StringBuilder sb = new StringBuilder(truck);
for (int index = 0; index < length; index++) {
    sb.append(0, " ");
}
truck = sb.toString();

You could write a method which produced an "empty" String

public String createPath(int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int index = 0; index < length; index++) {
        sb.append(" ");
    }
    return sb.toString();
}

Then simply append the result, for example...

truck = createPath(n) + truck;

Or you could be really tricky and simply use String.format...

public String createPath(int length) {
    return String.format("%" + length + "s", "");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thanks. I guess it is gonna take me some time to understand what is going on with everything. Thanks for your time.
If you lack a little experience with loops, consider taking a look at Control Flow Statements for more details
0

Without going into the nuances of capturing keypresses or any of that, you presumably just want a while loop to continue to "drive" your truck until the user quits (or whatever). A very simple one might look like this:

String truck = "<o><o>-<o>~|#|¬";
int count = 0;
while(true) { // This is your loop
    int n = 1; // or capture some user input, etc...

    // Then "move" the truck
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<n+count; i++) {
        sb.append(" ");
    }
    sb.append(truck);
    System.out.println(sb.toString());
    count = count + n;
}

Comments

0

This method generates the String you want to print. As you did not mention the way you want to listen to, it is omitted.

public String truck(int n) {
  String truck = "<o><o>-<o>~|#|¬";
  StringBuilder sb = new StringBuilder();
  for (int i = n; i-->0;)
    sb.append(' ');
  sb.append(truck);
  return sb.toString();
}

The loop in more ordinary form is:

  for (int i = 0; i < n; i++)
    sb.append(' ');

(I prefer the former when applicable, because the computation for n is done only once, and I have to specify the loop variable only once in the loop declaration.)

3 Comments

that is a very tricky for loop, had me double glancing that. Why not stick to the 3 part convention...
Although technically correct, you may want to tidy up your code (i.e. not using such a for loop, which the OP is unlikely to be able to understand) and also, add a summary as to *how8 it works.
©Quirliom, ©ns47731: Thanks for the feedback, I have updated the answer.
0

Try to do it this way:

public String truck(int countOfHits){
  StringBuilder sb = new StringBuilder("<o><o>-<o>~|#|¬");
  sb.reverse();
  for(int i = 0; i <= countOfHits; i++){
    sb.append(" ");
  }
  return sb.reverse().toString();
}

If you think about it, you realize that this will turn an O(n²) solution into O(n).

1 Comment

You just made an O(n) solution O(n^2) and than back to O(n) with 3 times larger constant. ;) If you prefer to make it fast, preallocate the length for the builder and append the truck at the end.

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.