Java的如何读取一个txt文件,使每一行它自己的字符串数组 [英] Java how to read a txt file and make each line it's own string array

查看:301
本文介绍了Java的如何读取一个txt文件,使每一行它自己的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务,我必须从一个文本文件中读取数据,并在每行总结的数据。下面是TXT文件:

I have an assignment where I have to read data from a text file and sum up the data in each line. Here is the txt file:

Chen Ruolin     9.2 9.3 9   9.9 9.5 9.5 9.6 9.8    
Emilie Heymans  9.2 9.2 9   9.9 9.5 9.5 9.7 9.6    
Wang Xin        9.2 9.2 9.1 9.9 9.5 9.6 9.4 9.8    
Paola Espinosa  9.2 9.3 9.2 9   9.5 9.3 9.6 9.8    
Tatiana Ortiz   9.2 9.3 9   9.4 9.1 9.5 9.6 9.8    
Melissa Wu      9.2 9.3 9.3 9.7 9.2 9.2 9.6 9.8    
Marie-Eve Marleau   9.2 9.2 9.2 9.9 9.5 9.2 9.3 9.8    
Tonia Couch     9.2 9   9.1 9.5 9.2 9.3 9.4 9.6    
Laura Wilkinson 9.7 9.1 9.3 9.4 9.5 9.4 9.6 9.2

名称和潜水分数的列表。我们应该从文件中读取数据,并把它放入数组(我猜每个行会是它自己的数组),滴每名潜水员的最高分和最低分,总结剩余的分数。因此,输出的第一行会是这个样子:

A list of names and diving scores. We're supposed to read the data from the file and put it into arrays (I'm guessing each line would be it's own array), drop the highest and lowest scores of each diver and sum up the remaining scores. So, the output for the first line would look something like this:

Chen Roulin: 56.90

我卡上的一点是如何采取线,并创建一个数组(一个字符串数组,在这种情况下),其中,将分离的文字和数字,并把它们每到一个数组索引。为了使它更清晰,第一行的阵列会是这样的:

The thing I'm stuck on is how to take a line and create an array (a string array, in this case) where it would separate the words and numbers and put them each into an array index. To make it more clear, the array for the first line would be something like:

String chenRoulin[] = {"Chen", "Roulin", "9.2", "9.3", "9", "9.9", "9.5", "9.5", "9.6", "9.8};

有了这个,我想我能传递给Double.parseDouble数值数组中,总结起来,我刚才解释。我知道二维数组,并已经尝试过这种方法,但被困在同样的问题,因为到目前为止,我已经找到了所有的例子都创建int或双阵列。基本上,这将是使一个数组出该文件的每一行的最佳/最简单的方法是什么?在此先感谢您的帮助。这里的code我迄今(它不是太大):

With this, I think I would be able to Double.parseDouble the numerical values in the array and sum them up as I explained earlier. I am aware of 2d arrays and have already tried that approach, but was stuck on the same problem, since all the examples I've found so far were creating int or double arrays. Basically, what would be the best/simplest way to make an array out of each line of the file? Thanks in advance for the help. Here's the code I have so far (it's not much):

import java.util.Scanner;
import java.io.*;

public class DivingData{  
  public static void main(String[] args) throws IOException {
    File myFile = new File("diving_data.txt");
    Scanner inputFile = new Scanner(myFile);

    String line = inputFile.nextLine();
    String[] chenRoulin = new String[10];


  }
}

只是要清楚,我只要求对这项任务的一个组成部分援助,而不是试图让这里有人做这一切对我来说。

Just to be clear, I'm only asking for assistance on one part of this assignment and not trying to get someone here to do it all for me.

推荐答案

您可以使用:

line.split("\\s+");

那么您检查您是否可以转换为加倍每个数组元素。恩pression\\ S +寻找空格分隔符像标签或空格字符。

then for each array element you check if you can convert to double. the expression "\s+" looks for white space delimiters like tabs or space char.

这篇关于Java的如何读取一个txt文件,使每一行它自己的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆