[英]Reading a file and storing names and numbers in two arrays
我正在开发一个程序,该程序读取文件并将名称和乐谱存储在两个单独的数组中,但是我很挣扎。 到目前为止,这就是我所拥有的。 我为名为“ names”的名称创建了一个数组,但是我很困惑如何将名称复制到数组的每个索引中。
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
首先,您需要在声明时将i
初始化为0
:
int i = 0;
然后,在分割线之后,您可以将数据从String[]
拉出,并将其放入您的names
和scores
数组中:
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()
无法将scores列中的数据解析为整数,则可能引发NumberFormatException
。
关于什么
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++;
}
我试图更正您的代码,并在发现您错了的地方提供内联注释。 实际上,您已经接近解决方案。 尝试找出一行代码之后的输出结果
String[] words = line.split("\t");
该行将提供两个String(因为它将拆分文件中仅包含一个制表符的名称和分数的行)。 您可以尝试自己调试。 就像简单地打印值一样。 例如
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.