Forums Register Login
can someone help me with this program
Could someone help me with this assignment?
I dont know how to get it to work
Ok I get what to do but the hard par is doing it
If anyone does help. thank you

Write a Java program that will calculate the reimbusement values and satisfy the following:

1. It reads data from a file and writes data to a file
2. The first line of the data file will contain the int of data values to process. after the first line of the data file, each line will contain a real number representing the number of miles. use a for loop to process the mileage values. Do not use an array
3. use an if/else if statement and the scale below to calculate the mileage reimbursement if the input value is > 0.
4. The main should contain all the variables - do not use a separate class for storage. Use at least the following methods: a. method to print the heading for the table b. method to outpup summary information. Also write two leftpad methods - one to return a String consisting of a real number with 1 decimal place padded on the left to a specified width and one where the real number has 2 decimal places.
5. There should be one line of output for each mileage value processed - use a table form for the output. The table should have a heading for the table and for each of the columns. The columns of the table should be lined up by the decimal point. Each detail line of the table will contain the number of miles (real - print with 1 dec place) and the reimbusement amt(real- print with 2 dec places). If the input value is <=0, then output 5stars in place of reimbursement amount.
6. After all the data values have ben processed, print the total of the reimbursement values, the number of mileage values processed, and the number of mileage values that were >=0. Include messages.
Data file
one number per line
10 40.2 799.6 -8.6 499.5 500.2 2999.4 0 1500.1 1239.8

Reimbursement scale:
Mileage Rate
up to 500 miles 15 cents per mile
500 to 1000 miles $75.0 + 12cents for each mile over 500
1000 to 1500 $135 + 10cents for each mile over 1000
1500 to 2000 $185 + 8cents for each mile over 1500
2000 to 3000 $225 + 6cents for each mile over 2000
over 3000 $285 + 5cents for each mile over 3000
What I have done is a mess
So far I got this:
//Java 5.0, Windows XP Pro

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class Pr6
public static void main (String [] args) throws Exception
{int totalamt = 0, ctr = 0;
double miles;
double amt;
String line;
String filename ="Pr6.dat";
StringTokenizer st;
PrintWriter fileOut = new PrintWriter(new FileOutputStream("Pr.out"));
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream(filename)));

for (int i = 0; i < n; i++)
{ //read line - get miles from it
line =br.readLine();
st = new StringTokenizer (line);
miles = Double.parseDouble(st.nextToken());
if (miles > 0)
if (miles < 500)
amt = .15 * (miles);
else if (miles < 1000)
amt = 75 + .12 * (miles - 500);
else if (miles < 1500)
amt = 135 + .10 * (miles - 1000);
else if (miles < 2000)
amt = 185 + .08 * (miles - 1500);
else if (miles < 3000)
amt = 225 + .06 * (miles - 2000);
else if (miles > 3000)
amt = 285 + .05 * (miles - 3000);
else if (miles <= 0)
amt = 0;
//heading of file
if (miles > 0)
fileOut.println (leftpad(miles,20)
+ leftpad2(amt,20));
fileOut.println (leftpad(miles,20)
+ "*******");
if (miles > 0)
totalamt = amt;
if (miles >= 0)
ctr = ++i;}
// Summary();
public static void head()
//heading of file
{ fileOut.println("# of MilesReimbursement");}
public static String leftpad(double miles,int w)
{DecimalFormat fmt = new DecimalFormat ("0.0");
String s = fmt.Format (miles);
int m = s.length();
for (int i = 0; i < w;w - m;i++)
s = " " + s;
return s;}
public static String leftpad2(double amt,int w)
{DecimalFormat fmt = new DecimalFormat ("0.00");
String s = fmt.Format (amt);
int m = s.length();
for (int i = 0;i < w;w - m;i++)
s = " " + s;
return s;}
//Summary Method
public static void Summary(double n, double totalamt, double ctr)

What exactly is your question? What does this code do differently than what you expect? Have you tested any of it?

My general advice would be to slow down. It appears that you wrote all this code, tried to run it, and then freaked out when it didn't work. The best way to write code is to write as little as possible, perhaps one line, then compile and test it. when you're sure that one line is working, THEN write another.

Granted, sometimes you have to write a little extra code to test what you've done, but in the end you'll save hours.

for example, the first thing i would do would be to write the code that handles the input file. you need to open it, read each line, and close it. i'd write a few extra lines that print each line just to be sure it was really working.

further tips:
use braces on all if loop constructs, even if the body is only one line.

when posting here, use the code tags. use the button below to insert the tags in your post, then paste your code between them. this will preserve your formatting, and make life easier for anyone who has time to help you.

ask a SPECIFIC question. rather than say "This doesn't work - fix it", say "i was expecting it to print ABC and it printed BC - can anybody tell me why?"

if you get an error message of some kind, either in the compilation or the execution, paste the error message here. they really do give you a lot of info, and help narrow down the search for what the problem might be.
Thank you for your help. I'll just slow down and do it step by step. but how am I supposed to do it when the whole program must work for part of it to work. Thanks! I'll take your advise
Sorry this is my first post so I wasn't sure what the tags were for.

Originally posted by Josh Diehl:
how am I supposed to do it when the whole program must work for part of it to work.

Typically, you don't need the WHOLE program to work. You should try to find some small part of the program that you can get to work on its own. In this case, reading a file and printing it to the screen is one possible place to start. If you focus on just this small part and make sure you understand how to read a file in Java, then you should be able to make some progress to completing the assignment. The next step might be to write the contents of one file to another location.

If you break up a programming assignment into little pieces like this, then you can concentrate on the details of that part. Once you get one piece working, you don't have to worry as much about it and can spend your time and energy working on the next piece. IMO, this is an important skill for any programmer to develop. In fact, as much as I like Java, I'd venture to say that it's more important than learning the Java language because it is very transferrable to other programming languges.

Keep Coding!

(Not to repeat what Layne just said, but he appears to have posted his comments as I was typing this up, so rather then letting it go to waste, I'll post it to add my two cents...)

Originally posted by Josh Diehl:
how am I supposed to do it when the whole program must work for part of it to work.

The first item in your requirement is to read from a file. That might be a good place to start. Write (and test) the code that does that; it should probably be its own method. Even that can be broken down so as to be written and tested in parts. Write the code to create the File object. Then test it. Write the code to find the file on your PC. Test it. Then write the code to open the file and read the contents (just reading it in, not necessary processing it in any way). Test it. What happens if the file does not exist?

Once you have that code done, select the next part to write. Since you are working on the file reading portion, the next best part to write might be the writing of the file. Test it. Once you have the reading and writing portion done, you have the start and end of your application, and can then work on the "guts" of it. Break the parts down. As you write the methods, you may need to test them separately from your file reading and writing code (by writing separate test methods or even test classes). Breaking a complex problem down into smaller problems, and those into smaller problems is the heart of development. Doing the same thing in the process of development will help you to learn to do such.

While all this may seem like a lot of extra work, as Fred comments, it actually saves you time in the long run. You can catch, find & isolate compilation errors and logic bugs very quickly. Which would you rather do. find a bug:
  • in 500 lines of code you just wrote over the course of the last few hours, having forgotten what you were thinking 3 hours ago when you wrote it
  • or in 5 lines you just wrote in the past 5 or 10 minutes?

  • By the way, while this way of programming has been around for some time, over time it has become a more formal and defined process; it is known as Test-Driven Development (TDD), or a "test first" development process. With this methodology, you actually write your test code first (which can be done using frameworks like JUnit) and then write your actual code. TDD is also the subject of the JavaRanch book Promotion from last week, Agile Java: Crafting Code with Test-Driven Development. Take a look through the beginners forum for some conversations with and comments by the author, Jeff Langr. I think you find them interesting.

    I hope that helps.

    [ March 14, 2005: Message edited by: Mark Vedder ]
    Thank you guys so much!I broke it down and tested the parts and corrected some bugs. I finally got it to compile with no errors but It looks like I still have some bug somewhere so I will go through it agian until It does what I would like it to do.
    Hey, sticks and stones baby. And maybe a wee mention of my stuff:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

    This thread has been viewed 1895 times.

    All times above are in ranch (not your local) time.
    The current ranch time is
    Jan 19, 2019 21:57:13.