• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.io.*;
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;
break;
case 2:
computeBill = acres*2;
break;
case 3:
computeBill = acres*4;
break;
case 4:
computeBill = acres*6;
break;
default:
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");

int
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");
}
else
{
System.out.println(clientNumber + " owes " + fmt.format(amountDue));
}
return "The total for all bills combined is " + fmt.format(amountDue);
scan.close();
}
}



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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic