Hello again, need some more help with java.

Discussion in 'Mac Programming' started by prostuff1, Jun 4, 2006.

  1. macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    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.

    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Don't use a static array. Use an ArrayList or Vector (look them up the Java docs). Both grow as required.
     
  3. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #3
    OK, thanks for the pointer.

    We have not learned that but I dont think she would care if I used that.
     
  4. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #4
    forget that. I forgot one of the <Integer> "tags" so it was giving me crap about that.
     
  5. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #5
    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
     
  6. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    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)?
     
  7. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #7
    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.
     

Share This Page