读取文件并将名称和数字存储在两个数组中

时间:2017-01-12 21:03:15

标签: java arrays file-io java.util.scanner

我正在研究一个读取文件的程序,并将名称和分数存储在两个独立的数组中,但我很挣扎。这就是我到目前为止所拥有的。 我为名称名称创建了一个数组,但我很困惑如何将名称复制到数组的每个索引中。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFileSplit {
     public static void main(String[] args) {

File file = new File("NamesScore.txt");
String[] names = new String[100];
int[] scores = new int[100];
int i;

 try {
      Scanner scanner = new Scanner(file);
      while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         String [] words = line.split("\t");
         for (String word: words) {
            System.out.println(word);
         }
       }
  } catch (FileNotFoundException e) {
     e.printStackTrace();
  }
 }
}

我的文字文件是:

John James      60
Kim Parker      80
Peter Dull      70
Bruce Time      20
Steve Dam       90

3 个答案:

答案 0 :(得分:1)

首先,您需要在声明时将i初始化为0

int i = 0;

然后,在分割线后,您可以从String[]中提取数据并将其放入namesscores数组中:

String [] words = line.split("\t");

// The first element in 'words' is the name
names[i] = words[0];

// The second element in 'words' is a score, but it is a String so we have
// to convert it to an integer before storing it
scores[i] = Integer.parseInt(words[1], 10);

// Increment 'i' before moving on to the next line in our file
i++;

如上所示,不要忘记增加i

有一些错误检查,我已经掩盖了。在致电split()后,您可能希望检查words的长度是否为2。另请注意,Integer.parseInt()如果无法将得分列中的数据解析为整数,则可以抛出NumberFormatException

答案 1 :(得分:1)

怎么样

  int l = 0;  
  while (scanner.hasNextLine()) {
     String line = scanner.nextLine();
     String [] words = line.split("\t");
     names[l] = words[0];
     scores[l] = Integer.parseInt(words[1]);
     System.out.println(l + " - name: " + names[l] + ", score: " + scores[l]);
     l++; 
   }

答案 2 :(得分:1)

我试图纠正您的代码并提供内联评论,我觉得您出错了。实际上你接近解决方案。尝试在一行代码(例如

)之后弄清楚你得到的输出结果
String[] words = line.split("\t");

这一行会给出两个字符串(因为它会分割你文件中只有一个制表符分隔名称和分数的行)。你可以尝试自己调试。就像打印价值一样。例如

System.out.println(words[0]);

这将有助于您进一步发展。

希望这有帮助。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TwoArrays {
    public static void main(String[] args) {
        File file = new File("C:\\test\\textTest.txt");
        String[] names = new String[100];
        int[] scores = new int[100];
        int i = 0;
        try {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] words = line.split("\t");
                names[i] = words[0]; // storing value in the first array
                scores[i] = Integer.parseInt(words[1]); // storing value in the
                                                        // second array
                i++;
            }
            /*
             * This piece of code will give unnecessary values as you have
             * selected an array of size greater than the values in the file for
             * 
             * for(String name: names) { 
             *      System.out.println("Name:- "+name);
             * }
             * for(int score: scores) { 
             *      System.out.println("Score:- "+score);
             *  }
             */
            // Better use the below way, here i am restricting the iteration till i
            // i is actually the count of lines your file have.
            for (int j = 0; j < i; j++) {
                System.out.println("Name:- " + names[j] + "\t" + "Score:- " + scores[j]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
相关问题