This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes for loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop problem" Watch "for loop problem" New topic
Author

for loop problem

Jr Winston
Greenhorn

Joined: Jul 22, 2011
Posts: 5
Hi. I am having a bit of problem with this program. You have to modify a bill program. The bill at the end should print out the bill including customer(s), account number, old and new meter readings, kilowatts per hour, and the amount due. I'm having problems putting a for loop to make the problem enter as many customers as the user wants to have. In addition, the user will first input the number of customers.

The code's errors:

ElectricBill4.java:106: <identifier> expected
System.out.println("Thank you. Goodbye.");
^
ElectricBill4.java:106: illegal start of type
System.out.println("Thank you. Goodbye.");
^
ElectricBill4.java:109: class, interface, or enum expected
}
^
3 errors


The code:



Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Please post your code in code tags, ensure proper indenting to make it easily readable.

Check your open and close curly brackets ({}), both for the proper number of pairs and their placement. What are you using for an editor?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Jr Winston
Greenhorn

Joined: Jul 22, 2011
Posts: 5
import java.text.DecimalFormat; // for decimal formatted output
import java.util.Scanner; // package needed for GUI

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

// variables
String Company; // to hold Company input
int AccountNumber; // to hold AccountNumber
String Customer;

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 ( Customer = 1; Customer <= 10; Customer++)

System.out.print("Enter your Customer: "); // Customer name

Customer = inputDevice.nextLine();

Customer = 1;
while(Customer < 11)
{
System.out.print(Customer);
++Customer;
}

System.out.println("State your company name: "); // Company name
Company = 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: " + Customer); // 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.

// input as many customers as you wish.

inputDevice.nextLine();

System.out.print("If you want to exit this program, please enter the word 'stop' in now."+ "\n"

+" Otherwise, enter your name: ");

Customer = inputDevice.nextLine(); // loops back to the Customer.
}

System.out.println("Thank you. Goodbye.");
}

}


I am using jGrasp as an editor.
Sultan Ali
Greenhorn

Joined: Jul 21, 2011
Posts: 7
Try working of this.
Sultan Ali
Greenhorn

Joined: Jul 21, 2011
Posts: 7
What i think you need to do is create a method with the information and then called that method in your main method using a while loop, this should solve you problem.... if you dont understand what i mean i will give you an example
e.g



dont forget to code your tags
Jr Winston
Greenhorn

Joined: Jul 22, 2011
Posts: 5
I am not sure what you mean. Can you give me an example? Thanks. Also, I ran the code and I compiled and ran it. When I enter a customer and I press return, it gave me an error: InputMismatchException.

I took the static keywords out because I didn't study that yet (that's the next chapter so I can't use that yet). I believe that the for loop is used to allow the user to enter as many customers as they desire, and I have to code it using the counting method. That is where I am stuck.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

If I take the code as you posted it, I can boil it down to this:


notice that your last System.out.println is OUTSIDE of the main method. You cannot do that. All executable code in java must be INSIDE a method.
Further, look at this line in your code:

for ( Customer = 1; Customer <= 10; Customer++)

Notice that you do not have an opening curly brace after it. That means that there is only ONE line in your for-loop body. I think you are simply missing the opening curly brace, and that will fix the above two issues.


now...You declare "Customer" as a String, but then try and use it as the index of your for-loop. You can't do that. The loop counter must be an integer type.

Try fixing those errors, and see what you end up with.


Note that this brings up a process issue. Whenever I write code, i NEVER write more than 1-2 lines before I try and compile/run/test/debug. You have over 100 lines that have clearly never been tested, so fixing all your mistakes could be a nightmare. You may want to consider starting over, and compile a heck of a lot more often.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You can get all sorts of problems from such tiny mistakes. The suggestions in our style guide may help prevent that happening again. Also, I have my own ideas about how you should write code, which may help you match your {}.

And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop problem
 
Similar Threads
Stumped, need inspiration.
reading input from a file into a array
Change Due/Tendered Program
Problem With Method and Interface
Very Confused!!