By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Question 'bout loops.

P: 6
Hi. I've got a problem that I'm hoping somebody here can solve:
Is it possible to put a boolean inside of a loop? The program I'm writing needs an error check built in (i.e. it will only loop if the user inputs 'y' or 'Y', program ends if user inputs 'n', and asks for user to re-enter the character if it's anything else.) Any comments or help is greatly appreciated....


BTW, if I need to put the source code up, let me know!


Thanks millions,
Jabberwiccy
Nov 15 '06 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Hi. I've got a problem that I'm hoping somebody here can solve:
Is it possible to put a boolean inside of a loop? The program I'm writing needs an error check built in (i.e. it will only loop if the user inputs 'y' or 'Y', program ends if user inputs 'n', and asks for user to re-enter the character if it's anything else.) Any comments or help is greatly appreciated....


BTW, if I need to put the source code up, let me know!


Thanks millions,
Jabberwiccy
Yeah let's have your code and show you were to do the touch ups
Nov 15 '06 #2

P: 6
Yeah let's have your code and show you were to do the touch ups
Alright, here it is...although it is a bit lengthy:
Expand|Select|Wrap|Line Numbers
  1. public class PowerBilling
  2. {
  3.     public static final double TAX = .035;
  4.     public static final double RATE = .25;
  5.  
  6.     public static void main (String [] args)
  7.     {
  8.         int monthnum, year, read1, read2, diff, ec1;
  9.         double charges, taxrate, total;
  10.         String month = " ";
  11.         String response = "y";
  12.         char yesno;
  13.         Scanner keyboard = new Scanner (System.in);
  14.  
  15.  
  16.         DecimalFormat threePlaces = new DecimalFormat ("000");
  17.         DecimalFormat fourPlaces = new DecimalFormat ("0000");
  18.         DecimalFormat fivePlaces = new DecimalFormat ("000.00");
  19.  
  20.  
  21.  
  22.         do
  23.             {
  24.             //Data entry for the bill
  25.             System.out.println ("Welcome to Power Billing\n\n");
  26.             System.out.print ("Enter Year: ");
  27.             year = keyboard.nextInt ();
  28.             System.out.print ("Enter Month: ");
  29.             monthnum = keyboard.nextInt ();
  30.             System.out.print ("Electricty Readings \n\t Enter previous reading: ");
  31.             read1 = keyboard.nextInt ();
  32.             System.out.print ("\t Enter current reading: ");
  33.             read2 = keyboard.nextInt ();
  34.             System.out.println (" ");
  35.  
  36.             //calculations
  37.             diff = read2 - read1;
  38.             charges = diff * RATE;
  39.             taxrate = charges * TAX;
  40.             total = charges + taxrate;
  41.  
  42.                 switch (monthnum)
  43.                 {
  44.                     case 1:        month = ("January");
  45.                                         break;
  46.                     case 2:        month = ("Febuary");
  47.                                         break;
  48.                     case 3:        month = ("March");
  49.                                         break;
  50.                     case 4:        month = ("April");
  51.                                         break;
  52.                     case 5:        month = ("May");
  53.                                         break;
  54.                     case 6:        month = ("June");
  55.                                         break;
  56.                     case 7:        month = ("July");
  57.                                         break;
  58.                     case 8:        month = ("August");
  59.                                         break;
  60.                     case 9:        month = ("September");
  61.                                         break;
  62.                     case 10:    month = ("October");
  63.                                         break;
  64.                     case 11:    month = ("November");
  65.                                         break;
  66.                     case 12:    month = ("December");
  67.                                         break;
  68.                     default:    System.out.print ("Blargh");
  69.                                         break;
  70.                 }
  71.  
  72.             //The bill itself
  73.             System.out.println ("The Power Company");
  74.             System.out.println ("Bill for " + month + ", " + year + "\n");
  75.             System.out.println ("\t\t Previous   Current   Usage   Charges");
  76.             System.out.println ("Electricity\t   " + (fourPlaces.format(read1))
  77.                                 + "\t     " + (fourPlaces.format(read2))//indented
  78.                                 + "      " + (threePlaces.format(diff)) //indented
  79.                                 + "     " + (fivePlaces.format(charges)));//indented
  80.             System.out.println ("Tax\t\t\t\t\t       " + (fivePlaces.format(taxrate)));
  81.             System.out.println ("\t\t\t\t\t      -------");
  82.             System.out.println ("Total\t\t\t\t\t       " + (fivePlaces.format(total)));
  83.             System.out.println ("\t\t\t\t\t      =======\n");
  84.             System.out.println ("Thank you for saving our precious resources!");
  85.             System.out.print ("Another bill?");
  86.             response = keyboard.next();
  87.             yesno = response.charAt (0);
  88.             if (yesno == 'y')
  89.                 ec1 = 1;
  90.             else if (yesno == 'n')
  91.                 ec1 = 0;
  92.             else if (yesno == 'Y')
  93.                 ec1 = 1;
  94.             else if (yesno == 'N')
  95.                 ec1 = 0;
  96.             else
  97.                 ec1 = 3;
  98.         }
  99.         while (if ec1 == 1)
  100.                 else if (ec1 == 0)
  101.                     System.out.print ("Reenter");
  102.                 else if (ec1 == 3)
  103.                     System.out.print ("blargh");
  104.     }
  105. }
  106.  
The stuff at the end is just me trying things out, and probably should be ignored...but still, any help is greatly appreciated.




Thanks,
Jabberwiccy
Nov 15 '06 #3

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.util.Scanner;
  4. import java.text.*;
  5. public class PowerBilling
  6. {
  7.  public static final double TAX = .035;
  8.  public static final double RATE = .25;
  9.  
  10.  public static void main (String [] args)
  11.  {
  12.   int monthnum, year, read1, read2, diff, ec1;
  13.   double charges, taxrate, total;
  14.   String month = " ";
  15.   String response = "";
  16.   char yesno = 'y';
  17.   Scanner keyboard = new Scanner (System.in);
  18.  
  19.  
  20.   DecimalFormat threePlaces = new DecimalFormat ("000");
  21.   DecimalFormat fourPlaces = new DecimalFormat ("0000");
  22.   DecimalFormat fivePlaces = new DecimalFormat ("000.00");
  23.  
  24.  
  25.  
  26.   while( yesno == 'y' || yesno == 'Y')
  27.    {
  28.    //Data entry for the bill
  29.    System.out.println ("Welcome to Power Billing\n\n");
  30.    System.out.print ("Enter Year: ");
  31.    year = keyboard.nextInt ();
  32.    System.out.print ("Enter Month: ");
  33.    monthnum = keyboard.nextInt ();
  34.    System.out.print ("Electricty Readings \n\t Enter previous reading: ");
  35.    read1 = keyboard.nextInt ();
  36.    System.out.print ("\t Enter current reading: ");
  37.    read2 = keyboard.nextInt ();
  38.    System.out.println (" ");
  39.  
  40.    //calculations
  41.    diff = read2 - read1;
  42.    charges = diff * RATE;
  43.    taxrate = charges * TAX;
  44.    total = charges + taxrate;
  45.  
  46.     switch (monthnum)
  47.     {
  48.      case 1:  month = ("January");
  49.           break;
  50.      case 2:  month = ("Febuary");
  51.           break;
  52.      case 3:  month = ("March");
  53.           break;
  54.      case 4:  month = ("April");
  55.           break;
  56.      case 5:  month = ("May");
  57.           break;
  58.      case 6:  month = ("June");
  59.           break;
  60.      case 7:  month = ("July");
  61.           break;
  62.      case 8:  month = ("August");
  63.           break;
  64.      case 9:  month = ("September");
  65.           break;
  66.      case 10: month = ("October");
  67.           break;
  68.      case 11: month = ("November");
  69.           break;
  70.      case 12: month = ("December");
  71.           break;
  72.      default: System.out.print ("Blargh");
  73.           //break;
  74.     }
  75.  
  76.    //The bill itself
  77.    System.out.println ("The Power Company");
  78.    System.out.println ("Bill for " + month + ", " + year + "\n");
  79.    System.out.println ("\t\t Previous   Current   Usage   Charges");
  80.    System.out.println ("Electricity\t   " + (fourPlaces.format(read1))
  81.         + "\t     " + (fourPlaces.format(read2))//indented
  82.         + "      " + (threePlaces.format(diff)) //indented
  83.         + "     " + (fivePlaces.format(charges)));//indented
  84.    System.out.println ("Tax\t\t\t\t\t       " + (fivePlaces.format(taxrate)));
  85.    System.out.println ("\t\t\t\t\t      -------");
  86.    System.out.println ("Total\t\t\t\t\t       " + (fivePlaces.format(total)));
  87.    System.out.println ("\t\t\t\t\t      =======\n");
  88.    System.out.println ("Thank you for saving our precious resources!");
  89.    System.out.print("Another bill? ");
  90.  
  91.    response = keyboard.next();
  92.  
  93.    yesno = response.charAt(0);
  94.    System.out.print(yesno);
  95.   }
  96.  
  97.  }
  98. }
  99.  
  100.  
How about this?
Nov 15 '06 #4

P: 6
How about this?
Thank you. Is there a way to, if the answer is, say, 'q' or some other letter, inform the user that this is not an appropriate response and prompt them again? (If that makes any sense?)
Nov 15 '06 #5

10K+
P: 13,264
Thank you. Is there a way to, if the answer is, say, 'q' or some other letter, inform the user that this is not an appropriate response and prompt them again? (If that makes any sense?)
You'd add something like

Expand|Select|Wrap|Line Numbers
  1.  
  2. yesno = response.charAt(0);
  3. while(yesno != 'y' || yesno != 'Y' || yesno != 'N') {
  4.      System.out.println("Invalid response. Please answer Y or N");
  5.      response = keyboard.next();
  6.      yesno = response.charAt(0); 
  7. }
Nov 16 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.