Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Well, finals week is finally here and I am just trying to finish off my last homework assignment that was given to me friday in my java programming class. Unfortunantly i am having some trouble with this last assignment.

The direction to explain what i need to do are here.

Just in case people dont want to look at that page here is the jist of what the program needs to do.

The program will perform the following actions:

1. Ask the user to enter the name of the input file containing the heart rate measurements, and input the file name;

2. Ask the user to enter the name of the output file where the output HTML document will be saved, and input the file name;

3. Open the input file by using a BufferedReader object (see Input file format for a description of the format of the input file);

4. Open (create) the output file by using a PrintWriter object (see Output file format for a description of the format of the output file);

5. Output to the file the appropriate opening HTML tags;

6. Output to the file the header row of the table;

7. For each series of heart rates in the input file, do the following:

1. Input the series of numbers from the input file and store them in an array;

2. Compute the minimum, maximum, and fitness quotient (i.e. twice the smallest heart rate in this series, divided by the sum of the smallest and largest heart rates) for the series;

3. Output one row of the HTML table to the output file, with the fitness quotient, minimum and maximum heart rates, and the list of heart rates;

8. Output to the file the appropriate closing HTML tags;

9. Close the input and output files.

Make sure you handle all possible IOException exceptions generated by the file I/O calls.

We were given a file (sample-input.txt) to work with and make our code work with but it should not be dependent on that file, obviously.

Right now all i have is this:
Code:
import java.util.Scanner;
import java.io.*;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		
	}
}

Which is the really easy part. My problems come when i need get stuff from the file. I can create a inputfile.readLine(); and print those just fine but whenever i try to creat an array to store them i have problems (i think mostly cause i dont knwo the length of the file). I am trying to store them so that i can refer to them later.

If anyone can point me in the right direction that would be great.

Thanks
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Ok, i am getting there thanks tot he suggestion

Anyway, here is the code i have so far:
Code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		ArrayList<Integer> numbersInFile = numbersinfile(inputfile);
		int numberOfCases = numbersInFile.get(0);
		int[] numbersPerPerson = numbersperperson(numbersInFile,numberOfCases);
		int[] maxHeartRate = maxheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		int[] minHeartRate = minheartrate(numbersInFile,numberOfCases,numbersPerPerson,maxHeartRate);
		double[] fitnessQuotient = fitnessquotient(numberOfCases,maxHeartRate,minHeartRate);
		for(int index = 0; index < numberOfCases; index++)
		{
			System.out.println(fitnessQuotient[index]);
		}
	}
	private static ArrayList<Integer> numbersinfile(BufferedReader inputfile) throws IOException
	{
		ArrayList<Integer> numbersInFile = new ArrayList<Integer>();
		String line = inputfile.readLine();
		while(line != null)
		{
			numbersInFile.add(Integer.parseInt(line));
			line = inputfile.readLine();
		}
		inputfile.close();
		return numbersInFile;
	}
	private static int[] numbersperperson(ArrayList<Integer> numbersInFile, int numberOfCases)
	{
		int[] numbersPerPerson = new int[numberOfCases];
		int count = 1;
		int index = 0;
		while(index < numberOfCases)
		{
			numbersPerPerson[index] = (numbersInFile.get(count));
			count = (count + numbersInFile.get(count)) + 1;
			index++;
		}
		return numbersPerPerson;
	}
	private static int[] maxheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] maxHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int max = 0;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) > max)
				{
					max = numbersInFile.get(between);
					maxHeartRate[index] = max;
				}
				between++;
				count++;
			}
			max = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return maxHeartRate;
	}
	private static int[] minheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson, int[] maxHeartRate)
	{
		int[] minHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int min = maxHeartRate[index];
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) < maxHeartRate[index])
				{
					min = numbersInFile.get(between);
					maxHeartRate[index] = min;
					minHeartRate[index] = min;
				}
				between++;
				count++;
			}
			min = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return minHeartRate;
	}
	private static double[] fitnessquotient(int numberOfCases, int[] maxHeartRate,int[] minHeartRate)
	{
		double[] fitnessQuotient = new double[numberOfCases];
		DecimalFormat formatter = new DecimalFormat("0.000");
		for(int index = 0; index < numberOfCases; index++)
		{
			fitnessQuotient[index] = ((2*minHeartRate[index])/(minHeartRate[index] + maxHeartRate[index]));
		}
		return fitnessQuotient;
	}
}

The one thing i can't get quite right is the fitness quotient to display correctly. I need it to be in the format "0.000." I created a decimal format like that but i can't get it to work. This is also the first time I have used deciaml formating. And my book does a piss poor job explaining it.

If someone could exaplin what i need to do that would be great.

Thanks
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
prostuff1 said:
The one thing i can't get quite right is the fitness quotient to display correctly. I need it to be in the format "0.000." I created a decimal format like that but i can't get it to work.
I don't know anything about Java decimal formatters, but it looks like you create the formatter but then never actually use it in any way with your fitness quotient variable. I assume you'd have to connect the formatter to the variable in some way (such as calling a method on the formatter and passing in the number you want formatted)?
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Thanks everyone for the help. Especially robbieduncan for the suggestion on using an ArrayList. Hopefully the teacher does nto complain to much about that since technically we had not learned it yet.

Anyway, if someone cares here is whhat the final code looks like:
Code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		int numbers = 2;
		int count2 = 0;
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.println();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		System.out.println();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		ArrayList<Integer> numbersInFile = numbersinfile(inputfile);
		int numberOfCases = numbersInFile.get(0);
		int[] numbersPerPerson = numbersperperson(numbersInFile,numberOfCases);
		int[] maxHeartRate = maxheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		int[] minHeartRate = minheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		double[] fitnessQuotient = fitnessquotient(numberOfCases,maxHeartRate,minHeartRate);
		outputfile.println("<html>");
		outputfile.println("<body>");
		outputfile.println("<table border=1 cellpadding=5");
		outputfile.println("<tr align=center>");
		outputfile.println("<th>Fitness Quotient</th>");
		outputfile.println("<th>Minimum Heart Rate</th>");
		outputfile.println("<th>Maximum Heart Rate</th>");
		outputfile.println("<th>Heart Rate Series</th></tr>");
		for(int index = 0; index < numberOfCases; index++)
		{
			outputfile.print("<tr><td align=center>" + fitnessQuotient[index] + "</td><td align=center>" + minHeartRate[index] + "</td><td align=center>" + maxHeartRate[index] + "</td><td>");
			while(count2 < numbersPerPerson[index] &&  numbers < numbersInFile.size())
			{
				outputfile.print(numbersInFile.get(numbers));
				if(count2 != (numbersPerPerson[index]-1))
				{
					outputfile.print(", ");
				}
				numbers++;
				count2++;
			}
			numbers = numbers + 1;
			count2 = 0;
		}
		outputfile.close();
		outputfile.println("</td></tr></table></body></html>");
		
	}
	private static ArrayList<Integer> numbersinfile(BufferedReader inputfile) throws IOException
	{
		ArrayList<Integer> numbersInFile = new ArrayList<Integer>();
		String line = inputfile.readLine();
		while(line != null)
		{
			numbersInFile.add(Integer.parseInt(line));
			line = inputfile.readLine();
		}
		inputfile.close();
		return numbersInFile;
	}
	private static int[] numbersperperson(ArrayList<Integer> numbersInFile, int numberOfCases)
	{
		int[] numbersPerPerson = new int[numberOfCases];
		int count = 1;
		int index = 0;
		while(index < numberOfCases)
		{
			numbersPerPerson[index] = (numbersInFile.get(count));
			count = (count + numbersInFile.get(count)) + 1;
			index++;
		}
		return numbersPerPerson;
	}
	private static int[] maxheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] maxHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int max = 0;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) > max)
				{
					max = numbersInFile.get(between);
					maxHeartRate[index] = max;
				}
				between++;
				count++;
			}
			max = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return maxHeartRate;
	}
	private static int[] minheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] minHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int min = 1000;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) < min)
				{
					min = numbersInFile.get(between);
					minHeartRate[index] = min;
				}
				between++;
				count++;
			}
			min = 1000;
			between = between + 1;
			count = 0;
			index++;
		}
		return minHeartRate;
	}
	private static double[] fitnessquotient(int numberOfCases, int[] maxHeartRate,int[] minHeartRate)
	{
		DecimalFormat formatter = new DecimalFormat("0.000");
		double[] fitnessQuotient = new double[numberOfCases];
		for(int index = 0; index < numberOfCases; index++)
		{	
			double fitnessquotient = (double)((double)(2*(double)minHeartRate[index])/(double)((double)minHeartRate[index] + (double)maxHeartRate[index]));
			fitnessQuotient[index] = Double.parseDouble(formatter.format(fitnessquotient));
		}
		return fitnessQuotient;
	}
}

To get this to work you need to download a file from the link in my first post.

Again, thanks for the help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.