为什么我的代码只读取 java 中 .txt 文件中的第一列数字
Why is my code only reading the first column of numbers in the .txt file in java
我正在尝试将 .txt 文件读入两个不同的数组,一个一维字符串数组和一个二维整数数组。代码将名称读入 NamesArray 就好了。但是,代码似乎要么只读取第一列数字,要么只输出第一列。我不确定代码哪里出了问题。非常感谢任何帮助!
下面的数据是它在文件中的格式。
Jason 10 15 20 25 18 20 26
Samantha 15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
Sheila 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21
这是我目前拥有的代码。
import java.io.*;
import java.util.*;
public class Page636Exercise12
{
// static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException, IOException
{
int Count = 0;
String[] NamesArray = new String[5];
int [][] MileageArray = new int [5][7];
int Mileage = 0;
String Names = "";
//Open the input file.
Scanner inFile = new Scanner(new FileReader("Ch9_Ex12Data.txt"));
//Reads from file into NamesArray and MileageArray.
while (inFile.hasNext())
{
Names = inFile.next();
NamesArray[Count] = Names;
Mileage = inFile.nextInt();
MileageArray[Count][Count] = Mileage;
Names = inFile.nextLine();
System.out.println(NamesArray[Count] + " " + MileageArray[Count][Count]);
Count++;
inFile.close();
}
}
这是我得到的输出结果。
Jason 10
Samantha 15
Ravi 20
Sheila 17
Ankit 16
问题是您只在索引 [count][count]
处的 MileageArray
中存储了一个数字。您将需要使用一个循环来填充数组,该循环针对预期的整数标记总数进行循环,如下所示:
for(int j = 0; j < 7; j++){
Mileage = inFile.nextInt();
MileageArray[Count][j] = Mileage;
}
Names = inFile.nextLine();
我还建议您将 MileageArray
列和行的大小指定为常量。即:
final int ROWS = 5;
final int COLS = 7;
如果输入文件格式发生变化,这将允许您以更可配置的方式更新解析代码。
您只读取了第一个整数
Mileage = inFile.nextInt();
您需要一个内部循环 运行 7 次来读取所有整数并将它们添加到二维数组中。
答案很简单,通过调用 Mileage = inFile.nextInt()
可以得到当前所在输入行的下一个标记,并将其解析为 int
,但由于数字由某些东西(假定为空格)仅读取和解析第一个数字。
您有多种选择来解决此问题。您可以:
解决方案 1
解决方案 2
try (BufferedReader br = new BufferedReader(
new FileReader( new File( "Performance_Results_310_13.dat" ) ) ))
{
String line;
while ( (line = br.readLine()) != null )
{
// split everything in a temporary array
String[] temp = line.split( "\s" );
// the name will always at the first position rest of the line are the numbers
name = temp[ 0 ];
nameArray[ count ] = name;
// or just simply nameArray[count] = temp[0]
// start at 1 since 0 is the name
for ( int i = 1; i < temp.length - 1; i++ )
{
// create a new array which is big enough for all numbers following
mileageArray[count] = new int[temp.length - 1];
mileageArray[ count ][ i - 1 ] = Integer.parseInt( temp[ i ] );
}
count++;
}
}
catch ( IOException e )
{
e.printStackTrace();
}
此解决方案读取整行,然后将其解析为所需格式。我认为它更灵活一些,但您必须确保数组足够大以存储数据。另请注意,此解决方案仅在空格后的每个标记都可解析为 int
(第一个除外)时才有效。
你需要带两个变量遍历一个二维数组,同时打印出来。您可以使用下面的代码,它会按预期工作。
public static void main(String[] args) throws FileNotFoundException, IOException{
int count = 0;
int j = 0;
String[] NamesArray = new String[5];
int[][] MileageArray = new int[5][7];
int Mileage = 0;
String Names = "";
// Open the input file.
Scanner inFile = new Scanner(new FileReader("Ch9_Ex12Data.txt"));
// Reads from file into NamesArray and MileageArray.
while (inFile.hasNext()) {
Names = inFile.next();
NamesArray[count] = Names;
j = 0;
System.out.print(NamesArray[count]);
while (inFile.hasNextInt())
{
Mileage = inFile.nextInt();
MileageArray[count][j] = Mileage;
System.out.print(" "+MileageArray[i][j]);
j++;
}
Names = inFile.nextLine();
count++;
System.out.println("");
}
inFile.close();
}
我正在尝试将 .txt 文件读入两个不同的数组,一个一维字符串数组和一个二维整数数组。代码将名称读入 NamesArray 就好了。但是,代码似乎要么只读取第一列数字,要么只输出第一列。我不确定代码哪里出了问题。非常感谢任何帮助!
下面的数据是它在文件中的格式。
Jason 10 15 20 25 18 20 26
Samantha 15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
Sheila 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21
这是我目前拥有的代码。
import java.io.*;
import java.util.*;
public class Page636Exercise12
{
// static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException, IOException
{
int Count = 0;
String[] NamesArray = new String[5];
int [][] MileageArray = new int [5][7];
int Mileage = 0;
String Names = "";
//Open the input file.
Scanner inFile = new Scanner(new FileReader("Ch9_Ex12Data.txt"));
//Reads from file into NamesArray and MileageArray.
while (inFile.hasNext())
{
Names = inFile.next();
NamesArray[Count] = Names;
Mileage = inFile.nextInt();
MileageArray[Count][Count] = Mileage;
Names = inFile.nextLine();
System.out.println(NamesArray[Count] + " " + MileageArray[Count][Count]);
Count++;
inFile.close();
}
}
这是我得到的输出结果。
Jason 10
Samantha 15
Ravi 20
Sheila 17
Ankit 16
问题是您只在索引 [count][count]
处的 MileageArray
中存储了一个数字。您将需要使用一个循环来填充数组,该循环针对预期的整数标记总数进行循环,如下所示:
for(int j = 0; j < 7; j++){
Mileage = inFile.nextInt();
MileageArray[Count][j] = Mileage;
}
Names = inFile.nextLine();
我还建议您将 MileageArray
列和行的大小指定为常量。即:
final int ROWS = 5;
final int COLS = 7;
如果输入文件格式发生变化,这将允许您以更可配置的方式更新解析代码。
您只读取了第一个整数
Mileage = inFile.nextInt();
您需要一个内部循环 运行 7 次来读取所有整数并将它们添加到二维数组中。
答案很简单,通过调用 Mileage = inFile.nextInt()
可以得到当前所在输入行的下一个标记,并将其解析为 int
,但由于数字由某些东西(假定为空格)仅读取和解析第一个数字。
您有多种选择来解决此问题。您可以:
解决方案 1
解决方案 2
try (BufferedReader br = new BufferedReader(
new FileReader( new File( "Performance_Results_310_13.dat" ) ) ))
{
String line;
while ( (line = br.readLine()) != null )
{
// split everything in a temporary array
String[] temp = line.split( "\s" );
// the name will always at the first position rest of the line are the numbers
name = temp[ 0 ];
nameArray[ count ] = name;
// or just simply nameArray[count] = temp[0]
// start at 1 since 0 is the name
for ( int i = 1; i < temp.length - 1; i++ )
{
// create a new array which is big enough for all numbers following
mileageArray[count] = new int[temp.length - 1];
mileageArray[ count ][ i - 1 ] = Integer.parseInt( temp[ i ] );
}
count++;
}
}
catch ( IOException e )
{
e.printStackTrace();
}
此解决方案读取整行,然后将其解析为所需格式。我认为它更灵活一些,但您必须确保数组足够大以存储数据。另请注意,此解决方案仅在空格后的每个标记都可解析为 int
(第一个除外)时才有效。
你需要带两个变量遍历一个二维数组,同时打印出来。您可以使用下面的代码,它会按预期工作。
public static void main(String[] args) throws FileNotFoundException, IOException{
int count = 0;
int j = 0;
String[] NamesArray = new String[5];
int[][] MileageArray = new int[5][7];
int Mileage = 0;
String Names = "";
// Open the input file.
Scanner inFile = new Scanner(new FileReader("Ch9_Ex12Data.txt"));
// Reads from file into NamesArray and MileageArray.
while (inFile.hasNext()) {
Names = inFile.next();
NamesArray[count] = Names;
j = 0;
System.out.print(NamesArray[count]);
while (inFile.hasNextInt())
{
Mileage = inFile.nextInt();
MileageArray[count][j] = Mileage;
System.out.print(" "+MileageArray[i][j]);
j++;
}
Names = inFile.nextLine();
count++;
System.out.println("");
}
inFile.close();
}