不断得到异常
Keep getting Exceptions
我正在尝试读取两个文件,匹配它们,然后打印另一个文件作为结果。但我一直收到这个 ArrayIndexOutOfBoundsException
。我不知道如何解决它,有人可以帮我吗?这是我的代码:
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
class Test{
final static char[][] DIG_CHAR = {{}, {}, {'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R', 'S'}, {'T', 'U', 'V'}, {'W', 'X', 'Y', 'Z'}};
public static void main(String[] args) throws IOException {
String s1 = "telephone.txt";
String s2 = "sample.txt";
Process(s1, s2);
}
public static void Process(String s1, String s2) throws IOException {
int size43 = ThreeCharacters(s2);
int size44 = FourCharacters(s2);
int size47 = SevenCharacters(s2);
String[] WordsOf3 = Store3Words(s2, size43);
String[] WordsOf4 = Store4Words(s2, size44);
String[] WordsOf7 = Store7Words(s2, size47);
String[] s = Char2Dig(WordsOf3);
String[] p = Char2Dig(WordsOf4);
String[] q = Char2Dig(WordsOf7);
Print3(WordsOf3, s, s1);
Print4(WordsOf4, p, s1);
Print7(WordsOf7, q, s1);
}
public static int ThreeCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 3) {
count++;
}
}
in.close();
return count;
}
public static int FourCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 4) {
count++;
}
}
in.close();
return count;
}
public static int SevenCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if(in.nextLine().length() == 7) {
count++;
}
}
in.close();
return count;
}
public static String[] Store3Words(String s2, int size) throws IOException {
//Here is where i keep getting the ArrayIndexOutOfBoundsException, probably same as the next two methods
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 3) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Store4Words(String s2, int size) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 4) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Store7Words(String s2, int size) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 7) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Char2Dig(String[] arr) { // ||
String temp = "";
String q = "";
String str = "";
for (int i = 0; i < arr.length; i++) {
temp = arr[i];
str = "";
for (int j = 0; j < temp.length(); j++) {
for (int m = 2; m < DIG_CHAR.length; m++) {
for (int k = 0; k < DIG_CHAR[m].length; k++) {
if (temp.charAt(j) == DIG_CHAR[m][k]) {
q = q + DIG_CHAR[m];
str = str + q;
break;
}
}
if (q == "") {
continue;
}
break;
}
q = "";
}
arr[i] = str;
}
return arr;
}
public static void Print3(String[] WordsOf3, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4, 7);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf3[i]);
}
}
}
outfile.close();
}
public static void Print4(String[] WordsOf4, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(7, 11);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf4[i]);
}
}
}
outfile.close();
}
public static void Print7(String[] WordsOf7, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf7[i]);
}
}
}
outfile.close();
}
}
问题是你没有算对。您错过了 3、4 或 7 个字符的数字行。让我们看看你的代码,方法 ThreeCharacters
:
public static int ThreeCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 3) {
count++;
}
}
in.close();
return count;
}
in.nextLine()
读取并跳过一行,然后 if (in.nextLine().length() == 3)
再次读取并跳过另一行。最后,您只检查两行中的一行。我猜建议是删除没有做任何事情的 in.nextLine()
。如果出于任何原因您故意放置它,请在 Store3Words
中添加相同的行,以便您也只处理两行中的一行。这将解决 ArrayIndexOutOfBoundsException
,但您的代码中可能还有其他问题,因为作为初学者,您没有遵循编程中应遵循的路径:编写一个方法,测试并确保它有效并转到下一个方法。
总之,我建议你写一个方法来计数,一个方法来存储,你的代码可以变得像这样紧凑:
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
class TestApp{
final static char[][] DIG_CHAR = {{}, {}, {'A', 'B', 'C'}, {'D', 'E', 'F'},
{'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R', 'S'},
{'T', 'U', 'V'}, {'W', 'X', 'Y', 'Z'}};
public static void main(String[] args) throws IOException {
String s1 = "telephone.txt";
String s2 = "sample.txt";
Process(s1, s2);
}
public static void Process(String s1, String s2) throws IOException {
int size43 = count(s2, 3);
int size44 = count(s2, 4);
int size47 = count(s2, 7);
/////
System.out.println("lines of 3: "+size43 );
System.out.println("lines of 4: "+size44 );
System.out.println("lines of 7: "+size47 );
/////
String[] WordsOf3 = store(s2, size43, 3);
String[] WordsOf4 = store(s2, size44, 4);
String[] WordsOf7 = store(s2, size47, 7);
/////
System.out.println("lines of 3" );
for(int i = 0; i<size43; i++){
System.out.println( WordsOf3[i] );
}
System.out.println("lines of 4" );
for(int i = 0; i<size44; i++){
System.out.println( WordsOf4[i] );
}
System.out.println("lines of 7" );
for(int i = 0; i<size47; i++){
System.out.println( WordsOf7[i] );
}
/////
String[] s = Char2Dig(WordsOf3);
String[] p = Char2Dig(WordsOf4);
String[] q = Char2Dig(WordsOf7);
Print3(WordsOf3, s, s1);
Print4(WordsOf4, p, s1);
Print7(WordsOf7, q, s1);
}
/** Single method that returns the number of lines of given number of char
* With this single method, no need to write ThreeCharacters, FourCharacters
* and SevenCharacters
*/
public static int count(String fName, int nChar) throws IOException {
Scanner in = new Scanner(new File(fName));
int count = 0;
String tmp; // Note the tmp variable, so that we do not advance twice
while (in.hasNextLine()) {
if (in.nextLine().length() == nChar) {
count++;
}
}
in.close();
return count;
}
/** Single method that returns all the lines of given number of char
* With this single method, no need to write Store3Words, Store4Words
* and Store7Words
*/
public static String[] store(String fName, int size, int nChar) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(fName));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == nChar) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Char2Dig(String[] arr) { // ||
String temp = "";
String q = "";
String str = "";
for (int i = 0; i < arr.length; i++) {
temp = arr[i];
str = "";
for (int j = 0; j < temp.length(); j++) {
for (int m = 2; m < DIG_CHAR.length; m++) {
for (int k = 0; k < DIG_CHAR[m].length; k++) {
if (temp.charAt(j) == DIG_CHAR[m][k]) {
q = q + DIG_CHAR[m];
str = str + q;
break;
}
}
if (q == "") {
continue;
}
break;
}
q = "";
}
arr[i] = str;
}
return arr;
}
public static void Print3(String[] WordsOf3, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4, 7);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf3[i]);
}
}
}
outfile.close();
}
public static void Print4(String[] WordsOf4, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(7, 11);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf4[i]);
}
}
}
outfile.close();
}
public static void Print7(String[] WordsOf7, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf7[i]);
}
}
}
outfile.close();
}
}
如果我理解 Print3
、Print4
和 Print7
背后的逻辑,我也会这样做。另请注意,我添加了一些打印语句以查看发生了什么。这对于查看您的代码是否正在执行它应该执行的操作非常有帮助。
我正在尝试读取两个文件,匹配它们,然后打印另一个文件作为结果。但我一直收到这个 ArrayIndexOutOfBoundsException
。我不知道如何解决它,有人可以帮我吗?这是我的代码:
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
class Test{
final static char[][] DIG_CHAR = {{}, {}, {'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R', 'S'}, {'T', 'U', 'V'}, {'W', 'X', 'Y', 'Z'}};
public static void main(String[] args) throws IOException {
String s1 = "telephone.txt";
String s2 = "sample.txt";
Process(s1, s2);
}
public static void Process(String s1, String s2) throws IOException {
int size43 = ThreeCharacters(s2);
int size44 = FourCharacters(s2);
int size47 = SevenCharacters(s2);
String[] WordsOf3 = Store3Words(s2, size43);
String[] WordsOf4 = Store4Words(s2, size44);
String[] WordsOf7 = Store7Words(s2, size47);
String[] s = Char2Dig(WordsOf3);
String[] p = Char2Dig(WordsOf4);
String[] q = Char2Dig(WordsOf7);
Print3(WordsOf3, s, s1);
Print4(WordsOf4, p, s1);
Print7(WordsOf7, q, s1);
}
public static int ThreeCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 3) {
count++;
}
}
in.close();
return count;
}
public static int FourCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 4) {
count++;
}
}
in.close();
return count;
}
public static int SevenCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if(in.nextLine().length() == 7) {
count++;
}
}
in.close();
return count;
}
public static String[] Store3Words(String s2, int size) throws IOException {
//Here is where i keep getting the ArrayIndexOutOfBoundsException, probably same as the next two methods
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 3) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Store4Words(String s2, int size) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 4) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Store7Words(String s2, int size) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(s2));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == 7) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Char2Dig(String[] arr) { // ||
String temp = "";
String q = "";
String str = "";
for (int i = 0; i < arr.length; i++) {
temp = arr[i];
str = "";
for (int j = 0; j < temp.length(); j++) {
for (int m = 2; m < DIG_CHAR.length; m++) {
for (int k = 0; k < DIG_CHAR[m].length; k++) {
if (temp.charAt(j) == DIG_CHAR[m][k]) {
q = q + DIG_CHAR[m];
str = str + q;
break;
}
}
if (q == "") {
continue;
}
break;
}
q = "";
}
arr[i] = str;
}
return arr;
}
public static void Print3(String[] WordsOf3, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4, 7);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf3[i]);
}
}
}
outfile.close();
}
public static void Print4(String[] WordsOf4, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(7, 11);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf4[i]);
}
}
}
outfile.close();
}
public static void Print7(String[] WordsOf7, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf7[i]);
}
}
}
outfile.close();
}
}
问题是你没有算对。您错过了 3、4 或 7 个字符的数字行。让我们看看你的代码,方法 ThreeCharacters
:
public static int ThreeCharacters(String s2) throws IOException {
Scanner in = new Scanner(new File(s2));
int count = 0;
while (in.hasNextLine()) {
in.nextLine();
if (in.nextLine().length() == 3) {
count++;
}
}
in.close();
return count;
}
in.nextLine()
读取并跳过一行,然后 if (in.nextLine().length() == 3)
再次读取并跳过另一行。最后,您只检查两行中的一行。我猜建议是删除没有做任何事情的 in.nextLine()
。如果出于任何原因您故意放置它,请在 Store3Words
中添加相同的行,以便您也只处理两行中的一行。这将解决 ArrayIndexOutOfBoundsException
,但您的代码中可能还有其他问题,因为作为初学者,您没有遵循编程中应遵循的路径:编写一个方法,测试并确保它有效并转到下一个方法。
总之,我建议你写一个方法来计数,一个方法来存储,你的代码可以变得像这样紧凑:
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
class TestApp{
final static char[][] DIG_CHAR = {{}, {}, {'A', 'B', 'C'}, {'D', 'E', 'F'},
{'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R', 'S'},
{'T', 'U', 'V'}, {'W', 'X', 'Y', 'Z'}};
public static void main(String[] args) throws IOException {
String s1 = "telephone.txt";
String s2 = "sample.txt";
Process(s1, s2);
}
public static void Process(String s1, String s2) throws IOException {
int size43 = count(s2, 3);
int size44 = count(s2, 4);
int size47 = count(s2, 7);
/////
System.out.println("lines of 3: "+size43 );
System.out.println("lines of 4: "+size44 );
System.out.println("lines of 7: "+size47 );
/////
String[] WordsOf3 = store(s2, size43, 3);
String[] WordsOf4 = store(s2, size44, 4);
String[] WordsOf7 = store(s2, size47, 7);
/////
System.out.println("lines of 3" );
for(int i = 0; i<size43; i++){
System.out.println( WordsOf3[i] );
}
System.out.println("lines of 4" );
for(int i = 0; i<size44; i++){
System.out.println( WordsOf4[i] );
}
System.out.println("lines of 7" );
for(int i = 0; i<size47; i++){
System.out.println( WordsOf7[i] );
}
/////
String[] s = Char2Dig(WordsOf3);
String[] p = Char2Dig(WordsOf4);
String[] q = Char2Dig(WordsOf7);
Print3(WordsOf3, s, s1);
Print4(WordsOf4, p, s1);
Print7(WordsOf7, q, s1);
}
/** Single method that returns the number of lines of given number of char
* With this single method, no need to write ThreeCharacters, FourCharacters
* and SevenCharacters
*/
public static int count(String fName, int nChar) throws IOException {
Scanner in = new Scanner(new File(fName));
int count = 0;
String tmp; // Note the tmp variable, so that we do not advance twice
while (in.hasNextLine()) {
if (in.nextLine().length() == nChar) {
count++;
}
}
in.close();
return count;
}
/** Single method that returns all the lines of given number of char
* With this single method, no need to write Store3Words, Store4Words
* and Store7Words
*/
public static String[] store(String fName, int size, int nChar) throws IOException {
String[] words = new String[size];
String temp = "";
Scanner in = new Scanner(new File(fName));
int i = 0;
while (in.hasNextLine()) {
temp = in.nextLine();
if (temp.length() == nChar) {
words[i] = temp;
i++;
}
}
in.close();
return words;
}
public static String[] Char2Dig(String[] arr) { // ||
String temp = "";
String q = "";
String str = "";
for (int i = 0; i < arr.length; i++) {
temp = arr[i];
str = "";
for (int j = 0; j < temp.length(); j++) {
for (int m = 2; m < DIG_CHAR.length; m++) {
for (int k = 0; k < DIG_CHAR[m].length; k++) {
if (temp.charAt(j) == DIG_CHAR[m][k]) {
q = q + DIG_CHAR[m];
str = str + q;
break;
}
}
if (q == "") {
continue;
}
break;
}
q = "";
}
arr[i] = str;
}
return arr;
}
public static void Print3(String[] WordsOf3, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4, 7);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf3[i]);
}
}
}
outfile.close();
}
public static void Print4(String[] WordsOf4, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(7, 11);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf4[i]);
}
}
}
outfile.close();
}
public static void Print7(String[] WordsOf7, String[] arr, String s1) throws IOException {
PrintWriter outfile = new PrintWriter(new FileWriter("result.txt"));
Scanner in = new Scanner(new File(s1));
String temp = "";
while (in.hasNextLine()) {
temp = in.nextLine().substring(4);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
outfile.println(temp + ": " + WordsOf7[i]);
}
}
}
outfile.close();
}
}
如果我理解 Print3
、Print4
和 Print7
背后的逻辑,我也会这样做。另请注意,我添加了一些打印语句以查看发生了什么。这对于查看您的代码是否正在执行它应该执行的操作非常有帮助。