使用 Puzzle 并弄清楚如何修复越界错误
Working With Puzzle and figuring how to fix out of bound error
My error Descriptions
我应该从一个文件中读取一个单词搜索难题我的问题我不断得到越界错误。我不知道如何根据文件的规范正确设置单词拼图的格式。拼图的格式通常是这样的:
5 5
d e v o l
red p h
qchzj
p o a f
v a m m n
q t f o x
这是我目前的工作,我觉得我已经从一个文件中读取下来,但将其转换为一个单词搜索谜题。特别是尽量不要对单词搜索拼图的行和列规范进行硬编码。
public static char[][] fill(){
// Created 2 different scanner one for user input and one to read the file
Scanner file1 = new Scanner(System.in);
// created a count to add the keywords
int count = 0;
//System.out.print("Please enter a keyword to search for.");
// Asking user to input a valid file name
System.out.print("Please enter a valid puzzle file name\nYou will be asked for the same file name again later.");
String wordFile = file1.nextLine();
FileReader infile;
boolean validFile = false;
// Creating a while loop that will keep asking for a valid file name
while(!validFile) {
// Using a try and catch to obtain correct file
try {
infile = new FileReader(wordFile);
file1 = new Scanner(infile);
validFile = true;
}
//ask the user to put a valid file name if they are wrong
catch(IOException e) {
System.out.println("Not a valid file name, please enter again!");
wordFile = file1.nextLine();
}
}
String numbers = file1.nextLine();
String[] fileArray = numbers.trim().split(" ");
int rows = Integer.parseInt(fileArray[0]);
int columns = Integer.parseInt(fileArray[1]);
char[][] fillThemLetters = new char [rows][columns];
String letters = file1.nextLine().trim().replace(" ", "");
char [] condensed = letters.toCharArray();
for (int i = 0; i < condensed.length; i++) {
for(int row = 0; row < rows; row++){
for(int column = 0; column < columns; column++)
{
char index = condensed[i];
fillThemLetters[row][column] = index;
i++;
}
}
}
return fillThemLetters;
}
您的索引越界错误是在这里造成的:
for(int column = 0; column < columns; column++)
{
char index = condensed[i];
fillThemLetters[row][column] = index;
i++; // <--------- THIS IS WRONG!!!
}
看到i++
了吗?您无缘无故地从最外层循环递增计数器。让循环处理它自己的增量,它已经内置,如下所示:
for (int i = 0; i < condensed.length; i++) { // <--- You already have i++ here!
解决这个问题后,您将 运行 遇到更多问题 -- 您的代码没有按您认为的那样运行,但这些都是单独的问题,应该这样发布分开。
My error Descriptions
我应该从一个文件中读取一个单词搜索难题我的问题我不断得到越界错误。我不知道如何根据文件的规范正确设置单词拼图的格式。拼图的格式通常是这样的:
5 5
d e v o l
red p h
qchzj
p o a f
v a m m n
q t f o x
这是我目前的工作,我觉得我已经从一个文件中读取下来,但将其转换为一个单词搜索谜题。特别是尽量不要对单词搜索拼图的行和列规范进行硬编码。
public static char[][] fill(){
// Created 2 different scanner one for user input and one to read the file
Scanner file1 = new Scanner(System.in);
// created a count to add the keywords
int count = 0;
//System.out.print("Please enter a keyword to search for.");
// Asking user to input a valid file name
System.out.print("Please enter a valid puzzle file name\nYou will be asked for the same file name again later.");
String wordFile = file1.nextLine();
FileReader infile;
boolean validFile = false;
// Creating a while loop that will keep asking for a valid file name
while(!validFile) {
// Using a try and catch to obtain correct file
try {
infile = new FileReader(wordFile);
file1 = new Scanner(infile);
validFile = true;
}
//ask the user to put a valid file name if they are wrong
catch(IOException e) {
System.out.println("Not a valid file name, please enter again!");
wordFile = file1.nextLine();
}
}
String numbers = file1.nextLine();
String[] fileArray = numbers.trim().split(" ");
int rows = Integer.parseInt(fileArray[0]);
int columns = Integer.parseInt(fileArray[1]);
char[][] fillThemLetters = new char [rows][columns];
String letters = file1.nextLine().trim().replace(" ", "");
char [] condensed = letters.toCharArray();
for (int i = 0; i < condensed.length; i++) {
for(int row = 0; row < rows; row++){
for(int column = 0; column < columns; column++)
{
char index = condensed[i];
fillThemLetters[row][column] = index;
i++;
}
}
}
return fillThemLetters;
}
您的索引越界错误是在这里造成的:
for(int column = 0; column < columns; column++)
{
char index = condensed[i];
fillThemLetters[row][column] = index;
i++; // <--------- THIS IS WRONG!!!
}
看到i++
了吗?您无缘无故地从最外层循环递增计数器。让循环处理它自己的增量,它已经内置,如下所示:
for (int i = 0; i < condensed.length; i++) { // <--- You already have i++ here!
解决这个问题后,您将 运行 遇到更多问题 -- 您的代码没有按您认为的那样运行,但这些都是单独的问题,应该这样发布分开。