Results 1 to 10 of 10
Thread: convertion to array
 11222009, 12:01 AM #1Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
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;
}
}

Your code is very hard to read. Please edit your post and add code tags. To see how to do this, look at my signature immediately below and click on the link. Again, you don't need to repost the code, just edit the post already present.
Much luck!
edit: also, what exactly are you having trouble with? Just posting a program without a very specific question is frowned upon here. You'll likely get a better answer if you can tell us exactly what the trouble is. Posting your best attempt at a solution using arrays will help with this.
 11222009, 12:30 AM #3Senior Member
 Join Date
 Sep 2009
 Location
 Sweden/Borås
 Posts
 107
 Rep Power
 0
Ocean  Hope you dont mind that i moved your code. Makes it little easer to read
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
Java Code: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; } }
 11232009, 09:46 AM #4Member
 Join Date
 Nov 2009
 Posts
 22
 Rep Power
 0
i code very much. how I don't know
:D I'm from vietnam  I hope you help me :D
 11232009, 07:42 PM #5Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
I'm sorry for not being specific with my question but i figured it would be good to mention that it has to be two parallel one dimensional arrays....I understand that i need to separate the amount and miles variables. my issue is that I don't know where to start. which is why i have no approximate guess as to how i should write this program.
edit this was as far as I have gotten but I'm pretty sure I'm way off from what it should look like
Java Code:import java.io.*; import java.util.*; import java.text.*; public class program9 { public static void main (String [] args) throws Exception { double totalmiles, totalamount = 0, total = 0; int[]amount = new int [12]; int[]miles = new int [12]; int ctr = 0; int ctrR = 0; String filename = ("mg.dat"); Scanner infile = new Scanner(new FileReader(filename)); PrintWriter outfile = new PrintWriter("mg.out"); header (outfile); int n = infile.nextInt(); for ( int i = 1; i <= n; i++) { miles[i] = infile.nextDouble(); if (miles > 0) ctrR++; if (miles[i] < 500) amount[i] = .15*miles; else if (miles[i] < 1000) amount[i] = 75.0 + .12*(miles  500); else if (j < 1500) amount[i]i = 135.0 + .10*(miles  1000); else if (miles[i] < 2000) amount[i] = 185.0 + .08*(miles  1500); else if (miles[i] < 3000) amount[i] = 225.0 + .06*(miles  2000); else amount[i] = 285.0 + .05*(miles  3000); if(miles[i] > 0) outfile.println(leftpad1(miles[i],10) + " " + leftpad2(amount[i],15)); else outfile.println(leftpad1(miles[i],10) + " " + " *****"); if (miles[i] > 0) total += amount[i]; if (miles[i] >= 0) ctr++;} // end of for loop summary (outfile, n, total, ctr);} //*************************************************************** 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("total amount " + (fmt.format(total))); 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[i], 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[i]); // 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[i], 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[i]); // 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; } }
Last edited by bobbychiken; 11252009 at 09:04 PM.
 11252009, 09:07 PM #6Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
iv been editing the code above to see if that helps anyone here but as previously stated i have no idea what im doing and im probably far off
 11252009, 09:50 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
When people use parallel arrays they are normally on the wrong track. Those arrays were used in Fortran code because Fortran didn't offer anything better; Java does:
Java Code:public class MilesThing { private int amount; private int miles; public MilesThing(int amount, int miles) { this.amount= amount; this.miles= miles; } public int getAmount() { return amount; } public int getMiles() { return miles; } }
kind regards,
Jos
 11292009, 05:04 AM #8Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
your input helped JosAH
and i also found some more input from another and maybe this code will be a bit closer to what it should look like ...I'm close to the final I'm more sure but I'm still missing a few things and if anyone could help me fill that in it would be most appreciated
Java Code:import java.io.*; import java.util.*; import java.text.*; public class program 9 { public static void main (String [] args) throws Exception { double totalMiles, totalAmount , totalmilesgt0, avgRe=0, avgMiles=0; double[]amounts = new double [15]; double[]miles = new double [15]; int ctrMiles = 0; int ctrMilesgt0 = 0; String filename = ("mg.dat"); Scanner infile = new Scanner(new FileReader(filename)); ctrMiles = getMiles(infile, miles); calcAmounts(miles, amounts, ctrmiles); totalMiles = calctotal(miles, ctrmiles); totalamount = calctotal(amounts, ctrmiles); totalmilesgt0 = calctotalgt0(miles, ctrmiles); totalamountgt0 = calctotalgt0(amounts, ctrmiles); ctrmilesgt0 = countValuesgt0(miles, ctrmiles); avgRe = totalamountgt0/b; avgMiles = totalmilesgt0/b; PrintWriter outfile = new PrintWriter("mg.out"); header (outfile); details (printWriter outfile,miles,amounts, ctrmiles); summary (outfile, n, total, ctr); }// end of body //************************************************************** public static double calctotal(double number[],int ctrmiles) { temp = 0; for (int i = 0; i < ctrmiles; i++) temp += number[i]; return temp; } //**************************************************************** public static double calctotalgt0(double number[],int ctrmilesgt0) { temp2 = 0; for (int i = 0; i < ctrmilesgt0; i++) { if(i > 0) temp2 += number[i]; } return temp2; } //**************************************************************** public static double countValues(double number[],int ctrmiles) { j = 0; for (int i = 0; i < ctrmiles; i++) { if ( i > 0) j++;} return j; } //**************************************************************** public static int getMiles(ctrMiles, double miles[]) { int n = infile.nextInt(); for ( int i = 0; i < n; i++) miles[i] = infile.nextDouble(); return n; } //*************************************************************** public static void details(PrintWriter outfile, double miles[], double amounts[],int numValues) { for(int i = 0;i < numValues;i++) { if(miles[i] > 0) outfile.println(leftpad1(miles[i],10) + " " + leftpad2(amount[i],15)); else outfile.println(leftpad1(miles[i],10) + " " + " *****"); } } //*************************************************************** public static double calcMiles(double mileage) { { if (mileage < 500) return .15*mileage; else if (mileage < 1000) return 75.0 + .12*(mileage  500); else if (mileage < 1500) return 135.0 + .10*(mileage  1000); else if (mileage < 2000) return 185.0 + .08*(mileage  1500); else if (mileage < 3000) return 225.0 + .06*(mileage  2000); else return 285.0 + .05*(mileage  3000); } } //*************************************************************** public static void calcAmounts(double miles[], double amounts[], int numValues) { for(int i = 0;i < numValues;i++) amounts[i] = calcMiles(miles[i]); } //*************************************************************** public static void header(PrintWriter outfile) { outfile.println (" Report"); outfile.println(" miles amount"); } //*************************************************************** public static void summary(PrintWriter outfile, int ctrmilesgt0, double totalamount, int ctrmiles) { outfile.println("total amount " + (fmt.format(total))); outfile.println(" The total amount of money received for all the miles run is " + totalamount); outfile.println(" The number of mileage values that were processed was " + ctrmilesgt0 + "."); outfile.println(" The number of mileage values that were greater than " + " or equal to zero was " + ctrmiles + "."); 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[i], 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[i]); // 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[i], 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[i]); // 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; } }
 12012009, 09:10 PM #9Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
I'm having trouble with a last few compile errors that i didn't think would be too hard but I'm just not seeing where to fix it. and I've tried fiddling with double and int where it relates but I'm not getting anywhere.
these are the errors and the code is below
program9.java:21: possible loss of precision
found : double
required: int
avgRe = (totalAmountgt0/ctrMiles);
^
program9.java:22: possible loss of precision
found : double
required: int
avgMiles = (totalMilesgt0/ctrMiles);
^
2 errors
Java Code:import java.io.*; import java.util.*; import java.text.*; public class program9 { public static void main (String [] args) throws Exception { double totalMiles, totalAmount , totalMilesgt0, totalAmountgt0 = 0; int avgRe, avgMiles =0; double[]amounts = new double [10]; double[]miles = new double [10]; int ctrMiles = 0; int ctrMilesgt0 = 0; String filename = ("mg.dat"); Scanner inFile = new Scanner(new FileReader(filename)); ctrMiles = getMiles(inFile, ctrMiles, miles); calcAmounts(miles, amounts, ctrMiles); totalMiles = calcTotal(miles, ctrMiles); totalAmount = calcTotal(amounts, ctrMiles); totalMilesgt0 = calcTotalgt0(miles, ctrMiles); totalAmountgt0 = calcTotalgt0(amounts, ctrMiles); avgRe = (totalAmountgt0/ctrMiles); avgMiles = (totalMilesgt0/ctrMiles); PrintWriter outFile = new PrintWriter("mg.out"); header (outFile); details (outFile, miles, amounts, ctrMiles); summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles); }// end of body //************************************************************** public static double calcTotal(double []number,int ctrMiles) { double temp = 0; for (int i = 0; i < ctrMiles; i++) temp += number[i]; return temp; } //**************************************************************** public static double calcTotalgt0(double []number,int ctrMiles) { int b = 0; for (int i = 0; i < ctrMiles; i++) { if(number[i] > 0) b += number[i]; } return b; } //**************************************************************** public static int getMiles(Scanner inFile , int ctrMiles, double []miles) { int n = inFile.nextInt(); for ( int i = 0; i < n; i++) miles[i] = inFile.nextDouble(); return n; } //*************************************************************** public static void details(PrintWriter outFile, double []miles, double []amounts,int numValues) { for(int i = 0;i < numValues;i++) { if(miles[i] > 0) outFile.println(leftpad1(miles[i],10) + " " + leftpad2(amounts[i],15)); else outFile.println(leftpad1(miles[i],10) + " " + " *****"); } } //*************************************************************** public static double calcMiles(double mileage) { { if (mileage < 500) return .15*mileage; else if (mileage < 1000) return 75.0 + .12*(mileage  500); else if (mileage < 1500) return 135.0 + .10*(mileage  1000); else if (mileage < 2000) return 185.0 + .08*(mileage  1500); else if (mileage < 3000) return 225.0 + .06*(mileage  2000); else return 285.0 + .05*(mileage  3000); } } //*************************************************************** public static void calcAmounts(double []miles, double []amounts, int numValues) { for(int i = 0;i < numValues;i++) amounts[i] = calcMiles(miles[i]); } //*************************************************************** public static void header(PrintWriter outFile) { outFile.println (" Report"); outFile.println(" miles amount"); } //*************************************************************** public static void summary(PrintWriter outFile, double totalAmount, int ctrMiles, int ctrMilesgt0, int avgRe, int avgMiles) { outFile.println(" The total amount of money received for all the miles run is " + totalAmount + "."); outFile.println(" The number of mileage values that were processed was " + ctrMiles + "."); outFile.println(" The number of mileage values that were greater than zero are " + ctrMilesgt0 + "."); outFile.println(" The averge reimbursement is " + avgRe + "."); outFile.println(" The average miles ran are " + avgMiles + "."); 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; } }
 12012009, 09:24 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Read the error message: if you do a division a/b where both a and (or) b are of type double, the quotient will be of type double. If you want to assign that result to something of type int you'll lose some of the precision. That is what the compiler is trying to tell you.
You either have to explicitly cast the result of the divisoin to type int to keep the compiler happy or you have to assign the result to something that also has type double (which also keeps the compiler happy).
kind regards,
Jos
Similar Threads

Program(convertion)
By ayangupta in forum Threads and SynchronizationReplies: 4Last Post: 03082009, 01:48 PM 
String/sentence to unicode convertion
By sandeepvreddy in forum New To JavaReplies: 5Last Post: 11202008, 04:33 PM 
stiring to unicode convertion
By sandeepvreddy in forum New To JavaReplies: 2Last Post: 09202008, 05:08 PM 
Problem with Image Convertion
By aptivo in forum Java 2DReplies: 0Last Post: 06092008, 11:15 PM 
Date convertion in java
By Preethi in forum New To JavaReplies: 25Last Post: 05162008, 05:11 PM
Bookmarks