View Single Post
  #1 (permalink)  
Old 11-22-2009, 12:01 AM
bobbychiken bobbychiken is offline
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
bobbychiken is on a distinguished road
Default convertion to array
I have a program that i have to convert to using arrays instead of what i have to do the same thing and i'm not the strongest with array can any one help?

... i had hoped to just attach this but it is too big so unfortunutly i have to just post it

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

public class program7
{ public static void main (String [] args) throws Exception
{ double miles, amount = 0, total = 0;
int ctr = 0;
String line;
String filename = ("mg.dat");
Scanner infile = new Scanner(new FileReader(filename));
StringTokenizer st;
DecimalFormat fmt = new DecimalFormat ("0.00");
PrintWriter outfile = new PrintWriter("mg.out");
outfile.println("Output File");
header (outfile);
int n = infile.nextInt();
for ( int i = 1; i <= n; i++)
{ miles = infile.nextDouble();
if (miles > 0)
if (miles < 500)
amount = .15*miles;
else if (miles < 1000)
amount = 75.0 + .12*(miles - 500);
else if (miles < 1500)
amount = 135.0 + .10*(miles - 1000);
else if (miles < 2000)
amount = 185.0 + .08*(miles - 1500);
else if (miles < 3000)
amount = 225.0 + .06*(miles - 2000);
else amount = 285.0 + .05*(miles - 3000);
if(miles > 0)
outfile.println(leftpad1(miles,10) + " " + leftpad2(amount,15));
else outfile.println(leftpad1(miles,10) + " " + " *****");
if (miles > 0)
total += amount;
if (miles >= 0)
ctr++;} // end of for loop
summary (outfile, n, total, ctr);
outfile.println("total amount " + (fmt.format(total)));
outfile.close();}
//************************************************** *************
public static void header(PrintWriter outfile)
{outfile.println (" Report");
outfile.println(" miles amount");}
//************************************************** *************
public static void summary(PrintWriter outfile, int n, double total, int ctr)
{outfile.println(" The total amount of money received for all the miles run is " + total);
outfile.println(" The number of mileage values that were processed was " + n + ".");
outfile.println(" The number of mileage values that were greater than " + " or equal to zero was " + ctr + ".");
outfile.close();
}
//************************************************** *************
// Returns miles formatted to one decimal place and padded by spaces
// on the left so that the String returned has length width

public static String leftpad1 (double miles, int width)
{ String s; // String to be returned
int m; // length of s
DecimalFormat fmt = new DecimalFormat("0.0");
// convert miles to a String with one decimal place
s = fmt.format(miles);
// determine the length of s
m = s.length();
// pad s by spaces on the left so that the resulting length of s is width
for (int i = 0; i < width - m; i++)
s = " " + s; // one space between the " "
return s;
}
//************************************************** *****************
// Returns amount formatted to one decimal place and padded by spaces
// on the left so that the String returned has length width

public static String leftpad2 (double amount, int width)
{ String s; // String to be returned
int m; // length of s
DecimalFormat fmt = new DecimalFormat("0.00");
// convert amount to a String with one decimal place
s = fmt.format(amount);
// determine the length of s
m = s.length();
// pad s by spaces on the left so that the resulting length of s is width
for (int i = 0; i < width - m; i++)
s = " " + s; // one space between the " "
return s;
}
}
Reply With Quote