如何拆分文本文件?
说一个文本文件有这样的行:
杰基尔 23.3 33.3 43.2
卡其色 32.5 54.3 43.2
......
到目前为止,对于我的代码,我有这个:
while(scan.hasNext())
{
String line = scan.nextLine();
String[] words = .split(" ");
for(int i = 1; i < words.length - 1; i++)
System.out.println(words[i]);
编辑
现在打印出来了,我的目标是将所有数字加在一起,然后创建一个平均值,我坚持这里需要做的事情
编辑整个代码:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
public class TemperaturesWeek2
{
public static void main( String [] args ) throws IOException
{
int count=0;
double averageTemp;
int averageAbove;
double highestAverage;
File inputFile = new File (
"C:/Users/Phillip/Documents/Temp/temperatures.txt .txt");
Scanner scan = new Scanner(inputFile);
while(scan.hasNext()) {
String words = scan.next();
double num1 = scan.nextDouble();
double num2 = scan.nextDouble();
double num3 = scan.nextDouble();
double num4 = scan.nextDouble();
double num5 = scan.nextDouble();
double num6 = scan.nextDouble();
double num7 = scan.nextDouble();
double average = (num1 + num2 + num3+ num4+ num5+ num6+ num7) / 7;
System.out.println(words);
System.out.println("average is : " + average);
}
}
}
}