File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to call data from a file in a while loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to call data from a file in a while loop?" Watch "How to call data from a file in a while loop?" New topic

How to call data from a file in a while loop?

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
I am trying to complete a program that reads input from a file and calculates the bill for different customers and then adds all of the total bills together. It must take in to account a 5% discount if there is work on over 1000 acres in addition to a 10% discount for any bill that exceeds $1500. I believe my first module of "public static double computBill" is correct, but I do not know how to file data in to the while loop in order to give discounts and I know that my second module of the "void main(string..." is incorrect. Or they could both be incorrect. Any help is much appreciated. Here is what I have so far:

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Assuming your file can contain multiple lines for one client, there is no way to calculate the discount based on total bill until you have processed all the lines in the file. Therefore you are going to have to accumulate information for each client as you go through the file. You don't say whether the "work on 1000 acres" has to be on one line in the file or not; if it does, you can calculate that in your "computeBill" routine, which appears to be called for one line. But after you're done accumulating things for all clients (either totalling them as you go or keeping an object per line organized by client), then you can see what their total bill is to give them the 10% discount.
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
Thanks Ralph! However, upon working on this more, I realized that I was focusing on the wrong problem, as I should have fixed the first module of computeBill first. Now that it is REALLY correct, I am having trouble understanding what my compiler is trying to tell me when it says "cannot find symbol - method grossCharge(int,int)" From what I understand, the code in the while loop needs to look just as it is (or if someone can tell otherwise, please let me know. Here is what I have now:

import java.util.*;
import java.text.NumberFormat;
public class farmBill
NumberFormat fmt = NumberFormat.getCurrencyInstance();
public static double computeBill
(int type, int acres, double computeBill, double acresDiscount,
double costDiscount, double grossCharge)
if (acres <= 0 || type < 4 || type > 1)
return -1 ;
switch (type)
case 1:
computeBill = acres*1;
case 2:
computeBill = acres*2;
case 3:
computeBill = acres*4;
case 4:
computeBill = acres*6;
computeBill = -1;
if (acres > 1000)
acresDiscount = -(acres * .05);
if (computeBill > 1500)
costDiscount = -(computeBill * .1);
return grossCharge = (computeBill + acresDiscount + costDiscount);

public static void main(String [] args) throws IOException
File input = new File("spraying.txt");

clientNumber, type, acres;
double computeBill;
double amountDue = 1;
double grossCharge;

Scanner scan = new Scanner(input);

while(clientNumber > 0)
type = scan.nextInt();
acres = scan.nextInt();
amountDue = grossCharge(type,acres);

if (amountDue < 0 || type < 4 || type > 1)
System.out.println("bad input data");
System.out.println(clientNumber + " owes " + fmt.format(amountDue));
return "The total for all bills combined is " + fmt.format(amountDue);

I am not concerned with getting the combined totals just yet. I have learned that programming can be a slow, step by step process, so I'll take one problem at a time from now on
Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hi Brian Mart,

The compiler is saying that you've made a call to method 'grossCharge' which takes 2 int arguments, but there's no definition found for that method. In short, you'll have to define that method.

Apart from that, I have some concerns about your code:
1) The instance variable fmt is - well - instance variable. So, you cannot use it in any static method. You are using it in main method, which will give you a compile time error.
2) Please follow Java coding convention.
3) In computeBill method, apart from type and acres, all other variables are simply used either intermediate value holders, or for holding the output. Their input values are never being used. So, why those variables are part of method signature? You can rewrite that method with accepting only two int variables.
4) In main method, you are not assigning clientNumber, type and acres variables. Please note that local variables in method are not initialized with default values. So, once you get rid of your initial compile time errors, you'll face further errors regarding usage of these variables without initialization.
5) You are invoking scan.close in main method after the return statement. This scan.close will never happen since it is after main statement.
6) You are returning a String from main, but main method is void.
7) In your if condition, there is a part : type < 4 || type > 1. This will always return true. What exactly are you trying to achieve?

I know that at this stage, you may not be aware of all these problems, but this questionnaire would be a nice exercise for you

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
I agree. Here's the link:
subject: How to call data from a file in a while loop?
It's not a secret anymore!