I must use a while loop but I need help figuring where.
Hi, I'm having a Java homework which is asking me to ask the user for the current population and it's average increase every day then I display the output. I'm asked to use a for loop and a while loop and use input validation. I successfully wrote the program but the issue is...I MUST use a while loop and I don't know how I can plug a while loop. I'm thinking of using the while loop as an outer loop for the for loop and I use it if the user entered the population number less than 2, days less than 1 or increase percentage less than 1. But, I'm really failing in doing it. When I put the while loop, it goes infinitely. And I'm instructed not to use a break statement for the while loop. I really don't know where or why I use a while loop but I just have to put it somewhere. Any ideas?
Thanks!
Re: I must use a while loop but I need help figuring where.
Code:
for(int i = 0; i < 5; i++){
//Stuff
}
Is equivalent to:
Code:
int i = 0;
while(i < 5){
//Stuff
i++;
}
Is equivalent to:
Code:
int i = 0;
do{
//Stuff
}while(++i < 5);
Hope that helps.
Re: I must use a while loop but I need help figuring where.
It looks like your class is using the same book that mine is. For that exercise, I did much like you are describing, using the while loop for the validation of the user input and a for loop for the actual increase in population calculation and display.
Re: I must use a while loop but I need help figuring where.
Alright, I've tried again and made this code.
But the problem is the output now, it seems like never ending. I mean, I don't see infinite stuff it just doesn't show the "Press any key to continue..." like always, rather it shows this _ and it keeps blinking.
Re: I must use a while loop but I need help figuring where.
Post some code ... using the proper code tags, of course.
Re: I must use a while loop but I need help figuring where.
Quote:
Originally Posted by
Keith Jackson
Post some code ... using the proper code tags, of course.
I did post my code twice as I believe you saw before. But unfortunately, it wouldn't be good if the code I submit can be found through Google. So, I left my code for a while then I had to remove it as a security measure. For example, the population number couldn't be less than 2.
So, what I did was this....
Ask for the number.
Get the number.
While the number is less than 2,
Display a message that says "Please enter 2 or more"
And then ask for the number again.
If it's 2 or more now, it passes the while loop and asks for the next one.
Thank you for your help and also thank you SJF.
Re: I must use a while loop but I need help figuring where.
Quote:
Originally Posted by
ScarabVenom
I did post my code twice as I believe you saw before. But unfortunately, it wouldn't be good if the code I submit can be found through Google. So, I left my code for a while then I had to remove it as a security measure. For example, the population number couldn't be less than 2.
So, what I did was this....
Ask for the number.
Get the number.
While the number is less than 2,
Display a message that says "Please enter 2 or more"
And then ask for the number again.
If it's 2 or more now, it passes the while loop and asks for the next one.
Thank you for your help and also thank you SJF.
One thing to consider is using the while loop to continuously check whether user input is correct. For instance, what happens if your program asks for a decimal number and someone feeds in text? I'm still learning Java so some things might not exactly be the best execution, but below is how I would handle it. The NumberFormatException is thrown when the user enters an invalid entry (such as a word when we're looking for an integer); it will also toss out any decimals...you can't have half of a person :)
Code:
import java.util.Scanner;
public class HomeworkExample {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean infoCorrect = false;
int theNumber = 0;
String userInput = "";
while (infoCorrect==false) {
System.out.println("Please enter a number higher than 2: ");
//try to read in the user input, if invalid code in "catch" executes
try {
userInput = keyboard.next();
//parse integer from string
theNumber = Integer.parseInt(userInput);
if (theNumber >= 2) {
System.out.println("Code to do whatever when the number is OK");
infoCorrect = true; // break out of while loop
} else {
System.out.println("Please enter a valid number");
}// if
} catch (NumberFormatException nfe) {
System.out.println("Please enter a valid number");
}// try
}// while
System.out.println("While loop exited - valid input entered");
keyboard.close(); //close the Scanner
}// main
}
Re: I must use a while loop but I need help figuring where.
Quote:
Originally Posted by
ScarabVenom
I did post my code twice as I believe you saw before. But unfortunately, it wouldn't be good if the code I submit can be found through Google. So, I left my code for a while then I had to remove it as a security measure. For example, the population number couldn't be less than 2.
So, what I did was this....
Ask for the number.
Get the number.
While the number is less than 2,
Display a message that says "Please enter 2 or more"
And then ask for the number again.
If it's 2 or more now, it passes the while loop and asks for the next one.
Thank you for your help and also thank you SJF.
Actually, I never saw your code or I would not have asked you to post it. :)-:
To keep this at a pseudo code type level, here is what I did. I used a do...while loop for each of the 3 inputs, (starting population, rate of growth, and days to grow.) Each loop basically said:
do
Get input from user
while input is not valid
Then I used the data from the user to set up a for...next loop to calculate the growth/population and output the daily results to the user.
Re: I must use a while loop but I need help figuring where.
You replied first when my code was in the thread. :) That was when you replied saying your first text. Then I posted another code again and left it for like 1-2 hours. Then I had to delete it since you know, you never know who's using Google to check after students.
I already submitted my assignment and I compiled it and it was working well. I'll wait till I see my grade.
Re: I must use a while loop but I need help figuring where.
Quote:
Originally Posted by
ScarabVenom
You replied first when my code was in the thread. :) That was when you replied saying your first text. Then I posted another code again and left it for like 1-2 hours. Then I had to delete it since you know, you never know who's using Google to check after students.
1. That's a gross misuse of a forum whose purpose is to share problems and their possible solutions.
2. Google caches search results for forever and a day. Deleting content here doesn't usually prevent it from showing up in a Google search.
For any future posts you make, a copy will be retained and any deleted code or other content will be restored and the thread will be closed. No further warning will be given.
db