我正在研究一個讀取文件並將名稱和分數存儲在兩個單獨數組中的程序,但我很掙扎。這是我迄今爲止所擁有的。 我創建了名稱名稱的數組,但我很困惑我將如何將名稱複製到數組的每個索引。讀取文件並將名稱和數字存儲在兩個數組中
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
什麼名字?你希望我們知道這個文件是什麼樣子的? – shmosel