473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Question 'bout loops.

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
5 1374
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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

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

Similar topics

14
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
5
by: Lada 'Ray' Lostak | last post by:
Dear list, First of all I want to say sory, if my question was answered somewhere. Or if it is my fault. If so, please, give me link/hint. My own search fails :( I have experimence with MySql,...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
13
by: John Salerno | last post by:
I'm interested in Programming Windows with C# (Core Reference) by Charles Petzold, but do you think that the new version of C# (along with .NET 2.0) will make this book obsolete in any way?
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
2
by: johnno | last post by:
Is anyone able to help me here? I have the following VB code and wish to have it rewritten in C++ but unsure how. Any help would be greatly appreciated. Effectively the code is packing a 10 letter...
10
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at...
19
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.