Mac figuring it out

speedracer5

macrumors newbie
Original poster
Jul 17, 2011
8
0
I have just begun using the for loop. Not sure how to use it in my program. Any help is appreciated.


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

public class ElectricPower
{
	public static void main(String[] args)
	{
		DecimalFormat num = new DecimalFormat("$,###.00");

		// variables
		String Company;							// to hold Company input
		String str;								// to hold username
		int AccountNumber;					// to hold AccountNumber
									
		double kwhUsed;
		double OldMeterReading; // string for inputting OldMeterReading	
		double NewMeterReading; // string for inputting NewMeterReading
		
		double AmountDue;
		double charge = 0.0;
						
		Scanner inputDevice = new Scanner(System.in); // initialize scanner	
		
		
		for( str =1; str < 11; str++)  // for loop to input as many customers as desired

				str = 1;
				while(str < 11)
				{
					System.out.println(str);
					++str;
				}
					
		System.out.println("State your company name: "); // Company name
		Company = inputDevice.nextLine();
					
		System.out.println("Please enter customer name: "); // CustomerName
		str = inputDevice.nextLine();

		System.out.println("Enter account number: "); // AccountNumber
		AccountNumber = inputDevice.nextInt();
				
		System.out.println("What is your old meter reading? "); // OldMeterReading);
		OldMeterReading = inputDevice.nextDouble();

		System.out.println("What is your new meter reading? "); // NewMeterReading);
		NewMeterReading = inputDevice.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
    		{
        		charge = 35.00+(0.02*(kwhUsed - 1000));
			}
			
			AmountDue = charge; // 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.
			 
			System.out.println(i);
			++i;
			
		   }
     }
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
8,664
4,935
vertical
You're not even close.

First, you haven't described what you want to happen.

Second, you haven't described what actually happened.

Third, you probably need to look at your C reference or textbook, because it's not at all clear what portion of the code you intend to be in the body of the for loop. If it's more than a single statement, then you need { } surrounding the block of code. Even if it's just a single statement, I recommend putting that statement in { }, to make it clear exactly where the boundaries of the loop are.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
0
Sydney
You're learning from Deitel & Deital Java : How to Program, right? You might want to review the chapters on control structures. In the 9th early-objects edition, that's chapters 4 and 5.
 

balamw

Moderator
Staff member
Aug 16, 2005
19,368
978
New England
It would help if you try to explain exactly what it is you are trying to accomplish and what the code you posted does that is not what you expected.

A more detailed thread title would be helpful like "(Java) help with for loops".

I didn't participate in your last thread so it might also be helpful to say what sources of material you are using to learn Java. "I'm learning for loops from XXX book"...

EDIT: chown33 and jiminaus are too fast for me. :p

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.