#I need help with this for loops question

1 messages ยท Page 1 of 1 (latest)

arctic pikeBOT
#

<@&987246399047479336> please have a look, thanks.

arctic pikeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

onyx ravine
#

u need another loop to repeat symbol x many times

#

๐Ÿ™‚

#

what's the code to print 5 stars

#

*****

#

put that code inside ur current code

#

then adjust the 5 to what tells u how many u need in this line

#

and done

#

step by step ๐Ÿ™‚

austere python
#

Kinda something like this:

String userInputChar
String loopGraphic = ""
for loop
{
    loopGraphic += userInputChar
    print loop iteration + " " + loopGraphic
}```
#

Essentially, you're concatenating a string every time you do loop

onyx ravine
#

that works as well, yes

#

also, Srring.repeat(...) is a thing

#

but i guess the assignment is to teach loops and hence is looking for the classic basic solution

austere python
#

Kind of, yeah

#

The term you're looking for is "concatenation"

onyx ravine
#

they just star with one star and then they add another star the next iteration, making it two, and so on

#

but really, this task is simple if u approach it step by step

#

u know how to write the code to print *****

#

so just take that code and put it inside ur current code

#

and boom ur done

#

that's the transfer this exercise wants u to make in ur head

austere python
#

Kind of, although you may need to add a variable

#

So, before the for loop, you create the string

#
String loopGraphic = "";```
#

If you make the variable inside of the loop, every time it loops, it's going to make the variable blank again

#

That's why we define it before the loop

#

Then, with the loop, every time it goes through once, you do something called "concatenation" which is defined as combining two strings into one

#

For example, if we had this piece of code:

String firstName = "Dev"
String lastName = "Dom"
System.out.print("Hello, "+firstName+" "+lastName);```
#

We initialize the first name and the last name, then in the print statement, we concatenate (or combine) a few strings: "Hello, ", the users first name, a space, then the last name

#

In that example, we concatenated strings

#

So, going back to the assignment, before the loop, you define the string that is going to be concatenated to

#

Then, every time it goes through the loop, we concatenate the user input to the variable

#

Let's go through the first iteration and assume the user input is *

#

We initialize the string and it's just blank: " "

#

Then, we can concatenate * to it

#

So " " + " * " = " * "

#

And then we print it

#

Then, let's do the second iteration

#

This time, when we do the concatenation, the variable is equal to " * "

#

And let's add another

#

" * " + " * " = " ** "

#

Alright, let's go to the third iteration

#

This time, the variable is equal to " ** "

#

Let's concatenate

#

" ** " + " * " = " *** "

#

And it keeps looping like that for as many times as you'd like

#

.
.
Does this help at all?

#

@uncut basalt

#

Is there any chance you could show the full error?

#

Or is that it?

#

Oh perfect

#

Wait then where did you see this?

#

Well yes LOL but I meant the specific picture

#

Where did the picture come from?

#

Ohh gotcha

#

Yeah that's fine in the context of this problem

#

Hmm.. Given this method, I'm not sure there is a way to get rid of the warning

#

The only other way to do this would be to nest another for loop inside of the for loop you currently have

onyx ravine
#

the warning is about using StringBuilder instead

#

sinxe += is a performance problem

austere python
#

Ohh that would make sense

unborn karma
#

actually, the += uses the StringBuilder class
so using the StringBuilder is faster

onyx ravine
#

and += isn't using stringbuilder

#

it was pre java 9, but even then the point was that it only applied it to the concat in this particular line, not across the loop - which is the actual performance problem

#

and hence the warning

unborn karma
onyx ravine
#

but that never applied beyond a single line

#

so u still have the issue in a loop like OP

#

and with java 9, this optimization was improved further, it's not using stringbuilder anymore

#

In any case, to get rid of the warning that OP got, they have to use stringbuilder across the loop

austere python
#

The more you know

unborn karma
#

ooooh ty