[英]Reading a text file into a char[][] array in Java
I am trying to fill a char[][] array from a text file and I cannot seem to figure out how to do it. 我正在尝试从文本文件填充char [] []数组,但似乎无法弄清楚该怎么做。 I've tried to use .toCharArray() but it doesn't seem to work. 我尝试使用.toCharArray(),但它似乎不起作用。 If you can give any insight on how to make this work that would be awesome! 如果您能提供有关如何完成这项工作的真知灼见,那就太好了!
String filename = "ArrayHW2.txt";
int numTests = 6;
char[][] testAnswers = new char[50][5];
char[] key = new char[4];
Scanner input = null;
try
{
input = new Scanner(new File(filename));
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening File");
System.exit(1);
}
for(int row = 0; row < testAnswers.length; row++)
{
for(int col = 0; col < testAnswers[row].length; col++)
{
testAnswers[row][col] = input.next().toCharArray();
}
}
input.close();
The basic problem is that you are trying to assign a character array to something was meant to hold a character. 基本问题是,您试图将字符数组分配给旨在容纳字符的对象。 You might think of char[]
type as storing the location in memory where characters are recorded, and a char
type as the character itself. 您可能会认为char[]
类型是将位置存储在记录字符的内存中,而char
类型是字符本身。
When you call toCharArray()
on a String
, the return type is char[]
. 当您在String
上调用toCharArray()
时,返回类型为char[]
。 It looks like you expect this array to have a single character, like the A, B, C, or D of a multiple-choice test. 您似乎希望该数组具有单个字符,如多项选择测试的A,B,C或D。 You could get the first (and only?) character of that array with something like ...toCharArray()[0]
, but this is wasteful because a new array is created, and characters are copied into it from the source string. 您可以使用...toCharArray()[0]
类的东西来获取该数组的第一个(也是唯一一个?)字符,但这很浪费,因为创建了一个新数组,并且从源字符串将字符复制到该数组中。 It's simpler to use the getCharAt()
method on the String
directly. 直接在String
上使用getCharAt()
方法更为简单。
String filename = "ArrayHW2.txt";
char[][] testAnswers = new char[50][5];
try (Scanner input = new Scanner(Paths.get(filename))) {
for(int row = 0; row < testAnswers.length; row++) {
for(int col = 0; col < testAnswers[row].length; col++) {
String token = r.next();
if (token.length() != 1)
throw new IllegalArgumentException("Answers must be one character");
testAnswers[row][col] = token.charAt(0);
}
}
} catch (IOException ex) {
System.err.println("Error reading file: " + ex.getMessage());
System.exit(1);
}
String filename = "ArrayHW2.txt";
int numTests = 6;
char[][] testAnswers = new char[50][5];
//char[] key = new char[4];
Scanner input = null;
try
{
input = new Scanner(new File(filename));
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening File");
System.exit(1);
}
int counter = 0;
while(scanner.hasNext())
{
copyString(testAnswers[counter], scanner.nextLine());
}
input.close();
its been a while i haven't code in java and im not sure about the methods but consider it a pseudo code . 一段时间以来,我还没有用Java编写代码,而且我不确定这些方法,但可以将其视为伪代码。 you can use this copy : 您可以使用此副本:
void copyString(char[] arr, String x)
{
int size = x.length();
for (int i=0; i<size; i++){
arr[i] = x.CharAt(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.