1

Ah yes, I am back with another Java question. So here I am supposed to repeat a string where its characters repeat a decreasing number of times. The first character should be repeated the string's length number of times.

Here is an example of what the output should look like:

HHHHH oooo www dd y 

What should I do next based on the code I have written below?

String go( String a) { String y = ""; for (int i = 0; i < a.length(); i++) { for (int j = 0; j < a.length(); j++) { y = y + a.charAt(i); } if (i == a.length() - 1) { y = y + ""; } else { y = y + "\n"; } } return y; } 

Feel free to point out any obvious mistakes I have made. I am new to Java and just learned that Java and Javascript are not the same thing!

2
  • 2
    Note that the creation of a string is a relatively expensive operation in Java, so if you're running in a loop, it's better to create a new StringBuilder() before the loop starts, use append inside the loop, and then use toString() after the loop is done than it is to use +. Commented Sep 17, 2020 at 4:45
  • Using a string builder will also simplify your code tremendously. Commented Sep 17, 2020 at 4:48

2 Answers 2

6

We can maintain two counters - 1 for extracting the character from string (characterLoc) and the other for specifying the number of times a character is to be repeated (repCount).

The outer while loop is used for extracting the character and inner loop is used for repeating the extracted character a specified number of times.

public static void main(String[] args) { String str = "Howdy"; int characterLoc = 0; int repCount = str.length(); while (characterLoc < str.length()) { for (int x = repCount; x > 0; x--) { System.out.print(str.charAt(characterLoc)); } characterLoc++; repCount--; System.out.println(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

This could be? How about convincing the readers, by adding an explanation? If you explain how this works and why it is supposed to solve OPs problem, then readers might consider it a solution. Even if you don't for some reason...
@yunnosch I literally watched the update happen. +1 to the community for helping each other get better.
@RichardBarker Thanks. This is of course what my goal was. I am trying different comments to get people to explain their code-onlys. Frankly I did not expect this one to be successful.... And the first upvote after edit was not even mine. Which makes me extra happy, because it confirms my idea that explanations make upvotable.
2

When I ran the code you posted in your question, I got this result:

HHHHH ooooo wwwww ddddd yyyyy 

which is not what you want.
In order to get what you want, you simply need to make one change in your code. You need to change the inner for loop. Here is your code with the required addition.

private static String go(String a) { String y = ""; for (int i = 0; i < a.length(); i++) { for (int j = 0; j < a.length() - i; j++) { // change here y = y + a.charAt(i); } if (i == a.length() - 1) { y = y + ""; } else { y = y + "\n"; } } return y; } 

When you run that code, it produces the following output.

HHHHH oooo www dd y 

which is what you want, isn't it?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.