嘿,所以瞄准一个程序,从一个文本文件中读取运动数据,并将该数据的摘要打印到另一个文本文件。输入文件包含多个记录 来自txt文件的示例输入如下所示:
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
我的问题是我无法让我的代码读取每个人我只能阅读第一个或者让我进入无限循环或输入不匹配。 继承我的代码:
`//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();
}
}
输出记录是输入记录的摘要。它由四个TAB分隔值组成:该参与者的数据天数,运动的总分钟数,每天运动的平均分钟数和丢失的总体重(如果参与者体重增加,则为负数) 。该名称已被读入,但不再打印出来(我们将数据匿名化)。在上面的记录中,受试者的名字是Joe Schmoe,他的起始体重是76.5kg。 Joe锻炼了四天(起始体重和最终体重之间有四个int值),总共85分钟(20 + 30 + 25 + 10 = 85)。他的最终体重是75.9公斤。输出记录是: 4 85 21.25 0.5999999999999943
我已经把我的大脑震撼了两个星期,任何帮助都会非常感谢谢谢
答案 0 :(得分:0)
无限循环:
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));
}
“;”在“while(inputStream.hasNext())”之后是一个空表达式,所以java只执行“;”而inputStream.hasNext()并不使用文件
下一个问题,InputMissmatchException: 你需要这行扫描仪:
inputStream =
new Scanner(new File("C:\\Test\\readTest.txt"));
inputStream.useLocale(Locale.ENGLISH);//this will help to interpret "." as decimal character
最后一个问题,你需要在这两行结束时消耗“换行符”字符:
lastWeight = inputStream.nextDouble();
outputStream.println(line + " " + firstWeight + " " + sum + " " + count + " " + lastWeight + " " + avg + " " + (lastWeight-firstWeight));
if(inputStream.hasNext())
inputStream.next(); // or even inputStream.nextLine();
现在我找到了第一个人: Joe Schmoe 76.5 85 4 75.9 21.0 -0.5999999999999943 这是正确的结果吗?