I am trying to change a program from an IF loop to a WHILE loop. The program started in Ch.05. It was modified in Ch.06 to an IF loop & now must be changed to a WHILE.
I got it down to 2 errors and am totally lost. Maybe a fresh set of eyes can point me in the right direction?
I've attached all 3 so you may see the progression.
Any assistance is greatly appreciated.
mikev9359 0 Newbie Poster
//Inputs are : Account #, Customer name, old and new meter reading
//Outputs : Company name (although there is no indications of a business input),
// Account #, Customer name, old & new meter readings, kwh used and Amount due
import javax.swing.*;
import java.text.*;// enables DecimalFormat.
public class Ch.05
{
public static void main (String args[])
{
double newUsage, amtDue, over1, over2;
String oldMtrStr, newUsageStr, cnameStr, newMtrStr;
cnameStr = JOptionPane.showInputDialog(null, "Enter Customer Name");
// bnameStr = JOptionPane.showInputDialog(null, "Enter Business Name"); if & when applicable this can be activated
String oldMtrString = JOptionPane.showInputDialog(null, "Enter Old Meter Reading");
String newMtrString = JOptionPane.showInputDialog(null, "Enter New Meter Reading");
String acctString = JOptionPane.showInputDialog(null, "Enter Account Number");
Double oldMtr = Double.valueOf(oldMtrString);
Double newMtr = Double.valueOf(newMtrString);
Double acct = Double.valueOf(acctString);
newUsage = (newMtr - oldMtr); // This calculates NEW usage by subtracting your old meter reading from your new reading
DecimalFormat Due = new DecimalFormat("$,###.00"); //This formats the Amount Due to 2 decimal places
// Here is where the calculations begin
if (newUsage < 301) // If your new meter reading is LESS THAN 301 you are charged a flat rate of $5.00
{amtDue=5.00;}
else
if(newUsage < 1001) // If you used more than 300 & less than 1000, 300 is subtracted from the New reading
{over1 = newUsage - 300; // The difference is taken and...
amtDue=(over1 * .03) + 5.00;} //then multiplied by 3 cents. This figure is added to the 5.00 for the total due
else //If you used over 1000 Kwh
{over2 = newUsage - 1000; // The difference is taken and it's
amtDue = (over2 * 0.02) + 35.00;}//then multiplied by 2 cents. $35.00 then added for your total due
JOptionPane.showMessageDialog(null,"Company Name (if applicable)" + //This confirms the Business (if applicable)
//This can replace the previous line if we decide to enter business names:(null,"Company Name " + bnameStr+
"\nYour Account number is " + acctString + // This confirms your Previous Acct # Input
"\nCustomer Name is " + cnameStr + //This confirms your Name
"\nNew Meter Reading is " + newMtrString + //This confirms your NEW METER Reading
"\nOld Meter Reading was" + oldMtrString + //This confirms your Old Meter Reading
"\nKwh Used is: " + newUsage + //This shows your Billable Usage of Kwhs
"\nThe TOTAL Amount Due is " + Due.format(amtDue));// This shows how much is due
System.exit(0);
}
}
import javax.swing.*;
import java.text.*;// enables DecimalFormat.
public class Electric61
{
public static void main (String args[])
{
double newUsage, amtDue, over1, over2; //Over1 is for KWH >300 but <1001 and Over2 is for KWH>1000
int again;
String oldMtrStr, newUsageStr, cnameStr, newMtrStr;
do
{
cnameStr = JOptionPane.showInputDialog(null, "Enter Customer Name");
// bnameStr = JOptionPane.showInputDialog(null, "Enter Business Name"); if & when applicable this can be activated
String oldMtrString = JOptionPane.showInputDialog(null, "Enter Old Meter Reading");
String newMtrString = JOptionPane.showInputDialog(null, "Enter New Meter Reading");
String acctString = JOptionPane.showInputDialog(null, "Enter Account Number");
Double oldMtr = Double.valueOf(oldMtrString);// Old Meter Reading
Double newMtr = Double.valueOf(newMtrString);// New Meter Reading
Double acct = Double.valueOf(acctString); //Account Number
newUsage = (newMtr - oldMtr); // This calculates NEW Usage by subtracting your old meter reading from your new reading
DecimalFormat Due = new DecimalFormat("$,###.00"); //This formats the Amount Due to 2 decimal places
// Here is where the calculations begin
if(newUsage < 301) // If new meter reading less old meter reading > 301 you're charged a flat rate of $5.00
{amtDue=5.00;}
else
if(newUsage < 1001) // If you used more than 300 & less than 1000, 300 is subtracted from the New reading
{over1 = newUsage - 300; // The difference is taken and...
amtDue=(over1 * .03) + 5.00;} //then multiplied by 3 cents. This figure is added to the 5.00 for the total due
else //If you used over 1000 Kwh
{over2 = newUsage - 1000; // The difference is taken and it's
amtDue = (over2 * 0.02) + 35.00;}//then multiplied by 2 cents. $35.00 then added for your total due
JOptionPane.showMessageDialog(null,"Company Name (if applicable)" + //This confirms the Business (if applicable)
//This can replace the previous line if we decide to enter business names:(null,"Company Name " + bnameStr+
"\nYour Account number is " + acctString + // This confirms your Previous Acct # Input
"\nCustomer Name is " + cnameStr + //This confirms your Name
"\nNew Meter Reading is " + newMtrString + //This confirms your NEW METER Reading
"\nOld Meter Reading was" + oldMtrString + //This confirms your Old Meter Reading
"\nKwh Used is: " + newUsage + //This shows your Billable Usage of Kwhs
"\nThe TOTAL Amount Due is " + Due.format(amtDue));// This shows how much is due
again = JOptionPane.showConfirmDialog(null,"Any Customer to Enter? ");
}
while(again == JOptionPane.YES_OPTION);
//displays the dialog box to put YES/NO
System.exit(0);
}
}
import javax.swing.*;
import java.text.*;// enables DecimalFormat.
public class Lab07
{
public static void main (String args[])
{
double newUsage, amtDue, over1, over2; // Over1 is for KWH >300 but <1001 and Over2 is for KWH>1000
int again;
String oldMtrStr, newUsageStr, cnameStr, newMtrStr;
do
{
cnameStr = JOptionPane.showInputDialog(null, "Enter Customer Name");
// bnameStr = JOptionPane.showInputDialog(null, "Enter Business Name"); if & when applicable this can be activated
String oldMtrString = JOptionPane.showInputDialog(null, "Enter Old Meter Reading");
String newMtrString = JOptionPane.showInputDialog(null, "Enter New Meter Reading");
String acctString = JOptionPane.showInputDialog(null, "Enter Account Number");
Double oldMtr = Double.valueOf(oldMtrString);// Old Meter Reading
Double newMtr = Double.valueOf(newMtrString);// New Meter Reading
Double acct = Double.valueOf(acctString); //Account Number
newUsage = (newMtr - oldMtr); // This calculates NEW Usage by subtracting your old meter reading from your new reading
DecimalFormat Due = new DecimalFormat("$,###.00"); //This formats the Amount Due to 2 decimal places
// Here is where the calculations begin
System.out.print( "Enter Customer Name or Stop to quit: " );// prompt
cname = input.nextLine(); // read employees name
// loop until sentinel value read from user
while ( cname != "Stop" )
{
if(newUsage < 301) // If new meter reading less old meter reading > 301 you're charged a flat rate of $5.00
{amtDue=5.00;}
else
if(newUsage < 1001) // If you used more than 300 & less than 1000, 300 is subtracted from the New reading
{over1 = newUsage - 300; // The difference is taken and...
amtDue=(over1 * .03) + 5.00;} //then multiplied by 3 cents. This figure is added to the 5.00 for the total due
else //If you used over 1000 Kwh
{over2 = newUsage - 1000; // The difference is taken and it's
amtDue = (over2 * 0.02) + 35.00;} //then multiplied by 2 cents. $35.00 then added for your total due
System.out.print("Customer Name " + //This confirms the Customer Name)
//Can be added if we use Business"\nBusiness Name is " + bnameStr + //This confirms your Name
"\nYour Account number is " + acctString + // This confirms your Previous Acct # Input
"\nNew Meter Reading is " + newMtrString + //This confirms your NEW METER Reading
"\nOld Meter Reading was" + oldMtrString + //This confirms your Old Meter Reading
"\nKwh Used is: " + newUsage + //This shows your Billable Usage of Kwhs
"\nThe TOTAL Amount Due is " + Due.format(amtDue));// This shows how much is due
//from IF program in Ch.06 again = System.out.print("Any Customer to Enter? ");replaced by 1st line of calculations
}
System.exit(0);
} // Lab07.java:51: while expected
} // Lab07.java:52: illegal start of expression
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
You are pretty close. The errors are because you still have mismatched braces on the loop blocks. You left the do() statement in, which is messing up your while() block. Walk through the flow of that while() block and you'll see that you need to prompt for another customer name at the end of it so the sentinel while() test can evaluate whether you want to continue or stop.
mikev9359 0 Newbie Poster
It's amazing what a fresh set of eyes can see.
I will be attempting it again tomorrow with the info you provided.
Thanks for your assist. Hopefully I'll be refeshed and 'clear-minded' to attempt this tomorrow.
bobbyfischer 0 Newbie Poster
Somebody save me i need someone to tell me how to create an if else loop for 10 different numbers
jon.kiparsky 326 Posting Virtuoso
I'd suggest you post this in a new thread rather than bouncing up an only slightly-related one from two years ago. When you do that, you might want to me a little more specific about what you want to do, and show us what you've tried and what's not working about it. That might get you a better response.
Before you do that, though, read the sticky threads at the top of this forum - if you haven't read Eric Raymond's piece on "How to Ask Questions the Smart Way" you're not done reading yet.
bobbyfischer 0 Newbie Poster
class doWhileDemo
{
public static void main(String arg[])
{
int x = 1;
int y = 2*x + x;
do {
System.out.println("y=:"+ y);
++x;
}
while(x<10);
}
}
am trying to parse the value of x after addition to the operation for y but it aint working
jon.kiparsky 326 Posting Virtuoso
If you didn't read my previous post, please read my previous post, and then start a new thread to ask your question.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.