figuring it out

Discussion in 'Mac Programming' started by speedracer5, Jul 20, 2011.

  1. speedracer5, Jul 20, 2011
    Last edited by a moderator: Jul 20, 2011

    speedracer5 macrumors newbie

    Joined:
    Jul 17, 2011
    #1
    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;
    			
    		   }
         }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    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.
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    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
     

Share This Page