• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to get a java program to repeat x amount of times automatically

 
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If someone could help me i am trying run a do while loop on java a specific amount of times for example if i were to type in "Enter number of years" and enter 4. i need the loop to execute 4 times or if i said 6 times then i would be 6. the program i have repeats it and takes it back to reentering "Enter number of years" i need it to execute by its self x amount of times. Please anyone can help? here is what i have so far

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your whole program? That one is incomplete.

Even with though it is incomplete, I believe your program's problem in the very last line you've posted:



Look closely at this test and ask yourself when it would be true and when it would be false.
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.text.DecimalFormat;
import java.util.Random;
/**
This class creates a program that --
*/
public class PgmTemplate {
/**
The main method is the program's starting point
*/
public static void main(String[] args)
{

int years;
int rainfall = 0;
double repeat;
//char repeat;
//int months; //max number of months shown

Scanner keyboard = new Scanner(System.in);
Random generator = new Random();

DecimalFormat formatter = new DecimalFormat("#,##0.00");

do
{
//ENtering number of years
System.out.println("Enter the number of years: ");
years = keyboard.nextInt();
rainfall = generator.nextInt(10) + 1;
System.out.println("Year " + "rainfall amounts were");;

for (int months = 1; months <= 12; months++)
{
System.out.println("Month " + months + " rainfall amount was " + formatter.format(rainfall));

}
}while(years >= years);

}

}


full program of what i have so far
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

But your while loop doesn't do anything.
You have a for loop, then immediately afterwards you wrote a while loop with an empty body, so that won't do anything. You shouldn't give it a tautology as a condition, but years >= years is a tautology.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell - it's a do-while loop. It's easier to see when properly formatted:

 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use decimal format. Look in the Java Tutorials and you will find something much better.

I added code tags to your first post. Always use them: doesn't it look better
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should make it if I wanted it to repeat itself automatically?
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bj Mar wrote:How should make it if I wanted it to repeat itself automatically?


Bj, look at Line 37 in fred's program listing. Can you describe in words what it is doing?
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:

Bj Mar wrote:How should make it if I wanted it to repeat itself automatically?


Bj, look at Line 37 in fred's program listing. Can you describe in words what it is doing?



I think It is reexecuting the program when the path is true of years == years
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the prgram, it reeexecutes the entire thing going all the way back to re enter years. What I want to to do it when I enter the number of years for distance 3 I want it to execute it 3 Times with random numbers.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bj Mar wrote:
I think It is reexecuting the program when the path is true of years == years


Bj, when would years == years not be true? That is the same as saying you are testing for 3 == 3, or 2.718281828 == 2.718281828.

When I asked you to describe in words what you want that test to do, I meant that you should try to decribe it without using any Java syntax (like the "==" operator, for example), and without using any Java variables (like "years"). The problem is, I think, that you aren't writing your Java code to match what it is that you want the program to do. That means you need to start by saying, in plain language (and, no problem if English isn't your best plain language; we're used to that here), what it is that you want your program to do.

Give it another try. Without using any Java, what do you want that line to do?
 
Sheriff
Posts: 28324
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain or give me an example on how i would get a program to execute its an X amount of times.
For instance if i say "Enter amount of years" and if i enter "3" what kind of loop and how would i go about making the loop execute 3 times. or if i eneter 5 years, how do i get it to execute 5 times.
PLEASE HELP i have an assignment do.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't this the same question that is being discussed here ?
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Isn't this the same question that is being discussed here ?



Yes, but i wanted to word it differently so maybe people could understand better what i mean
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your for loop in your code executes an exact number of times, so you already know the answer to this question.
You just need to use a variable in the condition (the middle part of the for loop) instead of a hard coded value.

Edited so it makes sense after Paul's merge
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Your for loop in the other thread executes an exact number of times, so you already know the answer to this question.
You just need to use a variable in the condition (the middle part of the for loop) instead of a hard coded value.



Can you show me an example of how you mean?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bj Mar wrote:Can you show me an example of how you mean?


This is your for loop

It will execute 12 times.

If you want to execute a loop a fixed number of times based on a variable you need something like


In your original code you were trying to do this with a while loop (which is okay, although for loops are generally used for a fixed number of repetitions), but you were trying to set the value of your loop variable inside the loop instead of before it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Bj Mar,
I have merged your topic into this topic. I hope that helps.


You may want to delete my first post (and maybe Bj's reply) otherwise people might go into an infinite loop
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Bj Mar wrote:Can you show me an example of how you mean?


This is your for loop

It will execute 12 times.

If you want to execute a loop a fixed number of times based on a variable you need something like


In your original code you were trying to do this with a while loop (which is okay, although for loops are generally used for a fixed number of repetitions), but you were trying to set the value of your loop variable inside the loop instead of before it.




Maybe i should word this better, when it executes 12 times thats what i want. After it finishses executing the 12 time i want it to come back right after and give a nother 12 months, or if i type in 3 years, total of 36 months executing
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bj Mar wrote:when it executes 12 times thats what i want. After it finishses executing the 12 time i want it to come back right after and give a nother 12 months, or if i type in 3 years, total of 36 months executing


So you need two for loops - one inside the other. The outer one loops X number of times and the inner one 12 times.
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Bj Mar wrote:when it executes 12 times thats what i want. After it finishses executing the 12 time i want it to come back right after and give a nother 12 months, or if i type in 3 years, total of 36 months executing


So you need two for loops - one inside the other. The outer one loops X number of times and the inner one 12 times.



Currently i have a for loop and then another nested for loop but i cant get it to do what i want.
 
B.J. Martin
Greenhorn
Posts: 12
Mac OS X Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind i just figured it out, thank you guys all for the help!
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Campbell - it's a do-while loop. . . .

Not in the code which was posted originally. The keyword do was omitted by mistake.

Anyway, whatever sort of loop it was, it was infinite because its continuation predicate was a tautology.
 
Hot dog! An advertiser loves us THIS much:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic