Question

Hey so aim working on a program that reads exercise data from a text file and prints a summary of that data to another text file. The input file consists of several records sample input from the txt file looks like this:

   1) Joe Schmoe
   76.5 20 30 25 10 75.9
   Jilly Momilly Schmill
   50.5 30 30 35 37 28 32 35 34 34 49.2
   Gail Quail
   62.3 15 17 10 3 10 63.6
   Frank Crank
   83.2 5 83.2
   John Quann
   57.9 30 32 35 32 30 57.2       

my problem is i can't get my code to read every person i can only get it to read the first one or send me into an infinite loop or an input-mismatch. heres my code:

`//Doubler.java
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.EOFException;
import java.io.IOException;

public class ExerciseSummary
{
    private Scanner inputStream = null;
    private PrintWriter outputStream = null;
    private int next = 0;
    private int sum = 0;
    private int count = 0;
    private double avg = 0;
    private double firstWeight = 0;
    private double lastWeight = 0;
    private String line;

    public static void main(String[] args)
        {
        ExerciseSummary summary = new ExerciseSummary();
        summary.connectToInputFile();
        summary.connectToOutputFile();
        summary.summaryData();
        summary.closeFiles();
        System.out.println("\nAll numbers from the input file have been");
        System.out.println("summarized and copied to the output file.");
    }

    public void connectToInputFile()
    {
        String inputFileName = getFileName("\nEnter name of input file: ");
        try
        {
            inputStream =
                new Scanner(new File(inputFileName));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File " + inputFileName + " not found.");
            System.exit(0);
        }
        catch(IOException e)
        {
            System.out.println("Error opening input file " + inputFileName);
            System.exit(0);
        }
    }

    private String getFileName(String prompt)
    {
        String fileName = null;
        System.out.print(prompt);
        Scanner keyboard = new Scanner(System.in);
        fileName = keyboard.next();
        return fileName; 
    }

    public void connectToOutputFile()
    {
        String outputFileName = getFileName("Enter name of output file: ");
        try
        {
            //outputStream = new PrintWriter(outFile);
            outputStream =
                new PrintWriter(outputFileName);
        }
        catch(IOException e)
        {
            System.out.println("Error opening output file " + outputFileName);
            //System.out.println(e.getMessage());
            System.exit(0);
        }
    }

    public void summaryData()
    {
        try 
        {
            while (inputStream.hasNext());
            {
                line = inputStream.nextLine();
                firstWeight = inputStream.nextDouble();
                while (inputStream.hasNextInt()) 
                {
                            sum += inputStream.nextInt();
                    count ++;
                    avg = (sum/count);
                    }
                lastWeight = inputStream.nextDouble();
                        outputStream.println(line + " " + firstWeight + " " + sum + " " +  count + " " +  lastWeight + " " + avg + " " + (lastWeight-firstWeight));
            }
        }
        catch (InputMismatchException e) 
        {
            System.out.print(e);
            System.exit(0);
        }
    }

    public void closeFiles()
    {
            inputStream.close();
            outputStream.close();
    }
}       

The output record is a summary of the input record. It consists of four TAB-separated values: the number of days of data for this participant, the total number of minutes exercised, the average minutes of exercise per day, and the total weight lost (which will be negative if the participant gained weight). The name has been read in, but not printed out again (we are anonymizing the data). For the record above, the subject's name was Joe Schmoe, and his starting weight was 76.5kg. Joe exercised four days (there are four int values between the starting weight and the final weight), for a total of 85 minutes (20 + 30 + 25 + 10 = 85). His final weight was 75.9kg. The output record is: 4 85 21.25 0.5999999999999943

i have been wracking my brain for two weeks any help would be so greatly appreciated thank you

Was it helpful?

Solution

infinite loop:

while (inputStream.hasNext());
        {
            line = inputStream.nextLine();
            firstWeight = inputStream.nextDouble();
            while (inputStream.hasNextInt()) 
            {
                        sum += inputStream.nextInt();
                count ++;
                avg = (sum/count);
                }
            lastWeight = inputStream.nextDouble();
                    outputStream.println(line + " " + firstWeight + " " + sum + " " + count + " " +  lastWeight + " " + avg + " " + (lastWeight-firstWeight));
        }

";" after "while (inputStream.hasNext())" is a empty expression, so java simply execute ";" while inputStream.hasNext() and don't consume the file

Next Problem, InputMissmatchException: you need this line for scanner:

inputStream =
            new Scanner(new File("C:\\Test\\readTest.txt"));
inputStream.useLocale(Locale.ENGLISH);//this will help to interpret "." as decimal character

and final problem, you need this 2 lines at the end of while to consume the "newline" character:

lastWeight = inputStream.nextDouble();
                    outputStream.println(line + " " + firstWeight + " " + sum + " " +  count + " " +  lastWeight + " " + avg + " " + (lastWeight-firstWeight));
if(inputStream.hasNext())
    inputStream.next(); // or even inputStream.nextLine();

now i get for the first person: Joe Schmoe 76.5 85 4 75.9 21.0 -0.5999999999999943 is it the right result?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top