Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

speedracer5

macrumors newbie
Original poster
Jul 17, 2011
8
0
Can anyone help me on this Java program I have? it's just about a do...while and it's driving me nuts. I got it to compile and work on the first run, but when I need to repeat the program is where I run into a slight problem. It's supposed to not bunch up in the repeat. here is what I am talking about when I repeat the output:

Would you like to prepare another bill for the next customer?
Enter Y for Yes or N for No: Y
State your company name: Please enter customer name: <--- this should be two lines.

My code:
Code:
import java.text.DecimalFormat; // for decimal formatted output
import java.util.Scanner; // package needed for GUI

public class Bill
{
	public static void main(String[] args) throws Exception	
	{
		DecimalFormat num = new DecimalFormat("$,###.00");
		
		
		// variables

		String Company;						// to hold Company input
		String str;								// to hold Customer
		int AccountNumber;					// to hold AccountNumber
		char repeat; 							// to hold yes or no, 'Y' or 'N'	

									
		double kwhUsed;			// kilowatts used per hour
		double OldMeterReading; // string for inputting OldMeterReading	
		double NewMeterReading; // string for inputting NewMeterReading
		
		double AmountDue;
		double charge = 0.0;
		
			
		Scanner input = new Scanner(System.in); 	// initialize scanner
	
		do
		{
			System.out.print("State your company name: "); // Company name
			Company = input.nextLine();
				
	 		System.out.print("Please enter customer name: "); // CustomerName
			str = input.nextLine();	
																			
			System.out.print("Enter account number: "); // AccountNumber
			AccountNumber = input.nextInt();
				
			System.out.print("What is your old meter reading? "); // OldMeterReading);
			OldMeterReading = input.nextDouble();

			System.out.print("What is your new meter reading? "); // NewMeterReading);
			NewMeterReading = input.nextDouble();
		
			// calculate the current electricity usage
		
			kwhUsed = (NewMeterReading - OldMeterReading);

											
			// computations

			if(kwhUsed <= 300)
			{
			charge = 5.00;
			}
			else if(kwhUsed <= 1000)
			{
			charge = 5.00 +(0.03*(kwhUsed - 300));
			}
			else if (kwhUsed >= 1001);
			{
			charge = 35.00 +(0.02*(kwhUsed - 1000));
			}
			
							
			AmountDue = (charge*kwhUsed); // calculate the bill
		
		
			System.out.println("Company name is: " + Company); // company name
				
			System.out.println("Customer name is: " + str); // customer
			
			System.out.println("Account number is: " + AccountNumber); // account number
	
			System.out.println("Old Meter Reading is: " + OldMeterReading); // old meter rating
	
			System.out.println("New Meter Reading is: " + NewMeterReading); // new meter rating
		
			System.out.println("Kilowatts per hour used is: " + kwhUsed);	// kilowatts per hour
	
			System.out.println("Amount Due is: " + num.format(AmountDue));	// amount due	
		
			System.out.println(); //Prints a blank line.
 
			// As many users as there are input
			System.out.println("Would you like to prepare another bill "+"for the next customer?");
 
 
			System.out.print("Enter Y for Yes or N for No: ");
 
 			str=input.next(); 		// Read next char
			repeat=str.charAt(0); // Get the first char.
 			
			} 
			while (repeat=='Y' || repeat=='y');
					
			System.out.println("Thank you. Goodbye.");
		} 
	}
 
Last edited by a moderator:
State your company name: Please enter customer name: <--- this should be two lines.

Not only should it be two lines, shouldn't you have to input something before the second line shows up?

Take a look at the methods for input you use in different situations, and also what values are actually assigned and when.
 
System.out.println prints a newline for you, print does not.

While that is true, it is completely unrelated to the actual problem.

If you look at how print and println are used, you'll notice that print is used consistently to make the input start on the same line, after the text, and println otherwise. My previous post should be enough to locate the actual problem.
 
Do you notice that you have to hit enter after hitting Y or N? That enter is in the input stream, but you don't read it after reading the Y or N. So when you next read from the input stream, first thing read is that Enter, hence the first prompt seems to be skipped after repeating.

Put another input.nextLine() after input.next() to read in the Enter.
Code:
str = input.next(); // Read next char
repeat = str.charAt(0); // Get the first char.
[COLOR=blue]input.nextLine(); // Skip over end-of-line[/COLOR]
 
How do I cut it down to two decisions for the selection control structure?

eg:

PHP:
// computations

			if(kwhUsed <= 300)
			{
			charge = 5.00;
			}
			else if(kwhUsed <= 1000)
			{
			charge = 5.00 +(0.03*(kwhUsed - 300));
			}
			else if (kwhUsed >= 1001);
			{
			charge = 35.00 +(0.02*(kwhUsed - 1000));
			}

It should be only the first two decisions, the else of the 2nd goes to the last computation.
 
Last edited by a moderator:
Like this?
Code:
if ( someCondition )
{  ...  }
else if ( otherCondition )
{  ...  }
else
{  .. every condition not previously accounted for ends up here .. }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.