Results 1 to 17 of 17

Thread: do while loop

  1. #1
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default do while loop

    can some one help me with the while part

    i need it to ask would you like to use again yes or no
    only any form of yes or no ex. YES yes YEs ect.
    but if something else is entered i need it to desplay not a valid choice
    if some one could help i would be thankful
    also thanks to every one that helped with my last question

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    the entire code or just the end

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    	do{
    			System.out.println("Enter year: ");
    			year = scan.nextInt();
    
    			if (year < 0)
    			{
    				System.out.println("Invalid quantity, only non-negitive numbers allowed.");
    			}
    		}while (year < 0);
    
    		do {
    
    			System.out.println("Enter month: ");
    			month  = scan.nextInt();
    
    			if (month < 1 | month >12)
    			{
    				System.out.println("");
    			}
    			else if(month == 1)
    			{
    				month2 = ("January");
    			}
    			else if(month == 2)
    			{
    				month2 = ("Febuary");
    			}
    			else if(month == 3)
    			{
    				month2 = ("March");
    			}
    			else if(month == 4)
    			{
    				month2 = ("April");
    			}
    			else if(month == 5)
    			{
    				month2 = ("May");
    			}
    			else if(month == 6)
    			{
    				month2 = ("June");
    			}
    			else if(month == 7)
    			{
    				month2 = ("July");
    			}
    			else if(month == 8)
    			{
    				month2 = ("August");
    			}
    			else if(month == 9)
    			{
    				month2 = ("September");
    			}
    			else if(month == 10)
    			{
    				month2 = ("October");
    			}
    			else if(month == 11)
    			{
    				month2 = ("November");
    			}
    			else if(month == 12)
    			{
    				month2 = ("December");
    			}
    		} while(month < 1 | month >12);
    
    		System.out.println("Electricity Readings");
    
    		do{
    			System.out.println("\t Enter previous reading: ");
    			reading1 = scan.nextInt();
    
    		if (reading1 < 0)
    		{
    			System.out.println("Invalid quantity, only non-negative numbers are allowed.");
    		}
    	} while (reading1 < 0);
    
    		do{
    			System.out.println("\t Enter current reading: ");
    			reading2 = scan.nextInt();
    
    
    	    if (reading1 > reading2)
    	    {
    			System.out.println("Invalid quantity, only numbers not less then the previous reading are allowed.");
    		}
    
    	} while(reading1 > reading2);
    
    	System.out.println("The Power Company");
    	System.out.println("Bill for " + month2 + ", " + year );
    	System.out.println("\t\t\tPrevious" + "\t\t\tCurrent" + "\t\t\tUsage" + "\t\t\tChareges");
    
    	final double tax = 0.35, rate = 0.25;
    	double usage = (reading2-reading1), charges = (usage * rate), taxpay = (tax * usage);
    	String bill;
    
    	System.out.println("Electricity" + reading1 + reading2 + usage + charges);
    	System.out.println("Tax" + "\t\t\t\t\t\t\t" + taxpay);
    	System.out.println("\t\t\t\t\t\t\t\t\----------");
    	System.out.println("Total" + "/t/t/t/t/t/t" + (taxpay + caharges));
    	System.out.println("\t\t\t\t\t\t\t\t\t\t\t========");
    	System.out.println("Thank you for saving are precious resources!");
    	
    	System.out.println("Another bill?")
    	bill = scan.nextInt();
    sorry that there are not any comments on it

  6. #6
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    oops posted before i read it

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to do something like this.

    Java Code:
            boolean status = true;
            Scanner in = new Scanner(System.in);
    
            while(status) {
                System.out.println("Are you sure you want to continue?");
                String str = in.nextLine();
    
                if(str.equalsIgnoreCase("yes")) {
                    // continue
                }
                else {
                    System.out.println("You enter an invalid choice.");
                    status = false;
                }
            }

  9. #9
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    at the end i need to end the original do with ta while that will ask if you want to use again and if yes it will go back to the beginning but if you put in any thing thats not yes or no it will say invalid choice
    sorry im trying to describe best i can im still very new to this as you can tell i probably could of used switch statement instead
    any ways did i clarify what i mean

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all go though the code I've post in my previous post and try to get an idea about the logic behind it. How it's handle basically. Then after try to bring that logic into your code.

  11. #11
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    so i kinda get the concept but how do i make it loop to the beginning because that just loops the question over and over
    also im not sure but do i need to add some kind of restrictions i think ! and && or something like that not to sure like i said

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually it's depends on your conditions need to check. I can see in tour code so many condition validations in where clause and so on.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Shall we start from the first condition you want to test. Then implement your model to continue/break the process.

  14. #14
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    so i kinda get the concept but how do i make it loop to the beginning because that just loops the question over and over
    also im not sure but do i need to add some kind of restrictions i think ! and && or something like that not to sure like i said
    whts your programs actual requirement. wht you want to get out of your program.?

  15. #15
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    y are you un-necessary checking all the condition in if-else ladder..just to identify which month is entered.

  16. #16
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    sorry im not that expiernced i have only been doing this for about 3 months so im still not that good yes i understand its quiet repetative i could of done switch but all i need is for after the program runs once it prompts the user if they want to use it again but if they enter anything besides yes or no that it will tell them its not a choice

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, if I'm correct the code what you've post here in 5th post is the one need to precess repetitively depends on the user response. If user says yes then, continue the process otherwise exit from the application

    Did you go through my code segment I've send to you?

    Java Code:
            boolean status = true;
    
            while(status) {
                   // Rest of the code
            }

    In while loop you can do the process. Until the status true the while is never exist and continuously executing. What happen if you are withing the code set the status to false? While loop exist and the execute rest of the code which has located after the while loop.

    You have to do the same. Is that clear to you?


Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 10:45 PM
  2. need a loop to add new row(s)
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 07:21 AM
  3. Which loop to use?
    By meee in forum New To Java
    Replies: 1
    Last Post: 09-07-2009, 10:36 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  5. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM

Posting Permissions

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