Java 读取文件并将名称和数字存储在两个数组中
我正在开发一个程序,它读取一个文件,并将名称和分数存储在两个单独的数组中,但我正在努力。这就是我目前所拥有的。 我为名为names的名称创建了一个数组,但我不知道如何将名称复制到数组的每个索引中Java 读取文件并将名称和数字存储在两个数组中,java,arrays,file-io,java.util.scanner,Java,Arrays,File Io,Java.util.scanner,我正在开发一个程序,它读取一个文件,并将名称和分数存储在两个单独的数组中,但我正在努力。这就是我目前所拥有的。 我为名为names的名称创建了一个数组,但我不知道如何将名称复制到数组的每个索引中 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerReadFileSplit { public static void main(S
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 [] 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
我已经掩盖了一些错误检查。您可能需要在调用后检查
单词的长度是否为2。还请记住,如果无法将分数列中的数据解析为整数,则可能会抛出一个。首先,在声明时,您需要将i
初始化为0
:
int i = 0;
然后,在拆分行之后,您可以从字符串[]
中提取数据,并将其放入名称
和分数
数组中:
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
我已经掩盖了一些错误检查。您可能需要在调用后检查单词的长度是否为2。还要记住,如果不能将分数列中的数据解析为整数,则可能抛出一个
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++;
}
那怎么办
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");
这一行将给出两个字符串(因为它将分割文件中只有一个选项卡分隔名称和分数的行)。你可以试着自己调试。就像简单地打印值一样。比如说
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();
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类数组{
公共静态void main(字符串[]args){
File File=新文件(“C:\\test\\textTest.txt”);
字符串[]名称=新字符串[100];
int[]分数=新的int[100];
int i=0;
试一试{
扫描仪=新扫描仪(文件);
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
String[]words=line.split(“\t”);
名称[i]=单词[0];//在第一个数组中存储值
分数[i]=Integer.parseInt(单词[1]);//将值存储在
//第二阵列
i++;
}
/*
*这段代码将提供不必要的值
*选择了一个数组,该数组的大小大于文件中的值
*
*对于(字符串名称:名称){
*System.out.println(“名称:-”+名称);
* }
*对于(整数分数:分数){
*System.out.println(“分数:-”+分数);
* }
*/
//最好使用下面的方法,这里我限制迭代直到
//我实际上是你的文件的行数。
对于(int j=0;j
我已尝试更正您的代码,并在我认为您出错的地方提供了内联注释。事实上,你已经接近解决方案了。在一行代码之后,试着找出作为输出得到的是什么,如
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();
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类数组{
公共静态void main(字符串[]args){
File File=新文件(“C:\\test\\textTest.txt”);
字符串[]名称=新字符串[100];
int[]分数=新的int[100];
int i=0;
试一试{
扫描仪=新扫描仪(文件);
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
String[]words=line.split(“\t”);
名称[i]=单词[0];//在第一个数组中存储值
分数[i]=Integer.parseInt(单词[1]);//将值存储在
//第二阵列
i++;
}
/*
*这段代码将提供不必要的值
*选择了一个数组,该数组的大小大于文件中的值
*
*对于(字符串名称:名称){
*System.out.println(“名称:-”+名称);
* }
*对于(整数分数:分数){
*System.out.println(“分数:-”+分数);
* }
*/
//最好使用下面的方法,这里我限制迭代直到
//我实际上是你的文件的行数。
对于(int j=0;j
什么名字?你希望我们知道这个文件是什么样子吗?什么名字?你希望我们知道这个文件是什么样子吗?