Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By SJF

Thread: I must use a while loop but I need help figuring where.

  1. #1
    ScarabVenom is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Angry 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!
    Last edited by ScarabVenom; 10-27-2012 at 06:57 AM.

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: I must use a while loop but I need help figuring where.

    Java Code:
        for(int i = 0; i < 5; i++){
            //Stuff
        }
    Is equivalent to:
    Java Code:
        int i = 0;
        while(i < 5){
            //Stuff
            i++;
        }
    Is equivalent to:
    Java Code:
        int i = 0;
        do{
            //Stuff
        }while(++i < 5);
    Hope that helps.
    DarrylBurke likes this.

  3. #3
    Keith Jackson is offline Member
    Join Date
    Sep 2012
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    Default 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.

  4. #4
    ScarabVenom is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default 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.
    Last edited by ScarabVenom; 10-29-2012 at 08:26 PM.

  5. #5
    Keith Jackson is offline Member
    Join Date
    Sep 2012
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    Default Re: I must use a while loop but I need help figuring where.

    Post some code ... using the proper code tags, of course.

  6. #6
    ScarabVenom is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: I must use a while loop but I need help figuring where.

    Quote Originally Posted by Keith Jackson View Post
    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.

  7. #7
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: I must use a while loop but I need help figuring where.

    Quote Originally Posted by ScarabVenom View Post
    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 :)

    Java 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
    }
    Last edited by DrummondAW; 10-30-2012 at 03:44 AM.

  8. #8
    Keith Jackson is offline Member
    Join Date
    Sep 2012
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    Default Re: I must use a while loop but I need help figuring where.

    Quote Originally Posted by ScarabVenom View Post
    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.

  9. #9
    ScarabVenom is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: I must use a while loop but I need help figuring where.

    Quote Originally Posted by ScarabVenom View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. multiline listview need help figuring this out
    By Darkyere in forum Android
    Replies: 0
    Last Post: 10-22-2012, 09:00 PM
  2. need help figuring out algorithm
    By mnorman in forum New To Java
    Replies: 8
    Last Post: 12-15-2011, 01:41 AM
  3. Help figuring out a regular expression!
    By srs in forum New To Java
    Replies: 1
    Last Post: 12-10-2011, 02:51 AM
  4. Figuring out for????
    By seanfmglobal in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:47 PM
  5. Help with figuring out the output
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •