• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help making this code loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey very new to java. wanting to get this to loop 7 times but no guides seem to help me. i can get loop functions to compile correctly but it doesn't want to loop after the body code is executed

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Loops in Java include the for-loop, the enhanced for-loop (aka the for-each loop), while-loop, and do-while loop.  These are all explained in the Official Java Tutorial under the Control Flow Statements section

Your code doesn't have any of these loops so it's hardly surprising that the execution doesn't repeat.
 
James Jameson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep I omitted them because they didn't work. I was hoping someone could show me how to implement the loop code to my existing work as every guide I have seen shows just the loop code and not how to incorporate it in an existing code, at least in a way that I can understand
 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to Java too, so please take my advice lightly.

I think your variables need to be declared inside the main method


but I also noticed you declared them again inside the method and initialized them too. So basically you have two String studentName variables. You can't have two with the same name.


As for loops, there's three choices: while, do while, and for loop. With any of these you'll need some kind of iterator variable to tell the loop when to stop running.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Jameson wrote:Yep I omitted them because they didn't work.


Around here, saying that ItDoesntWorkIsUseless (←click that, it's a link).  Please TellTheDetails (←another link). What did you write, what were you expecting the program to do, and what did it actually do?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suppose you put your code  between do while loop and try.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rajith: As much as possible, try to write idiomatic code.  That is, use program constructs that most people would use or expect you to use in a given situation. When you have a known or fixed number of iterations, a regular for-loop is what most people will expect to see.  A while- or a do-while loop is one where the number of iterations can't really be determined beforehand but is conditional on something that happens inside the loop body.

In the case of wanting to execute a block of code 7 times, a for-loop is more idiomatic.
 
James Jameson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This is my code currently. How do I add the for loop to make this code repeat 7 times? I've tried google and I'm just not understanding the guides on the for loop
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, please understand that we don't encourage other forum members to give out solutions to problems that you're still working on. We prefer that you figure things out yourself.

What guides/tutorials have you looked at and what don't you understand? Did you go over the official Java Tutorial pages that I pointed you to earlier? Those are pretty clear and the examples given are simple enough for even beginners like you to understand.

If you ShowSomeEffort you're more likely to get the kind of help most people around here are more than happy to give.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you missed it, here's the link to the Official Java Tutorial on Control Flow statements (includes loops): https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simple example:

This prints out "Hello!" one time. To do this multiple times, you can use a for-loop like this:

This prints "Hello!" 10 times, each time on a separate line (because println() will print a new line after it prints its arguments).

So any code that's inside the pair of braces that immediately follow the for loop header (everything starting from the keyword for and the close parenthesis) will be repeated.
 
James Jameson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you I was close with my previous tries. Now to calculate the average of 7 students marks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic