#I need help with this for loops question
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
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 ๐
I don't believe they need another loop, they just need to add something inside the for loop
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
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
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
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
Ohh that would make sense
actually, the += uses the StringBuilder class
so using the StringBuilder is faster
not sure what u mean by that. u seem to contradict urself in the same sentence
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
maybe yes, I remember hearing about that before but I am probably wrong
before java 9, the compiler replaced any + by stringbuilder, to cover situations like
= a + b + c + d
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
The more you know
ooooh ty