Java 不断有例外
我试图读取两个文件,匹配它们,然后打印另一个文件作为结果。但我一直在使用这个Java 不断有例外,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我试图读取两个文件,匹配它们,然后打印另一个文件作为结果。但我一直在使用这个数组索引outofboundsException。我不知道怎么修,有人能帮我吗?这是我的密码: 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
数组索引outofboundsException
。我不知道怎么修,有人能帮我吗?这是我的密码:
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();
}
}
import java.io.IOException;
导入java.util.Scanner;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.PrintWriter;
课堂测试{
最后一个静态字符[][]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'};
公共静态void main(字符串[]args)引发IOException{
字符串s1=“telephone.txt”;
字符串s2=“sample.txt”;
过程(s1,s2);
}
公共静态无效进程(字符串s1、字符串s2)引发IOException{
int size43=三个字符(s2);
int size44=四个字符(s2);
int size47=七个字符(s2);
字符串[]WordsOf3=Store3Words(s2,大小43);
字符串[]WordsOf4=Store4Words(s2,大小44);
字符串[]WordsOf7=存储7个单词(s2,大小47);
字符串[]s=Char2Dig(WordsOf3);
字符串[]p=Char2Dig(WordsOf4);
字符串[]q=Char2Dig(WordsOf7);
打印3(字SOF3、s、s1);
印刷品4(字SOF4、p、s1);
打印7(字SOF7、q、s1);
}
公共静态int三个字符(字符串s2)引发IOException{
扫描仪输入=新扫描仪(新文件(s2));
整数计数=0;
while(在.hasNextLine()中){
in.nextLine();
if(in.nextLine().length()==3){
计数++;
}
}
in.close();
返回计数;
}
公共静态int-FourCharacters(字符串s2)引发IOException{
扫描仪输入=新扫描仪(新文件(s2));
整数计数=0;
while(在.hasNextLine()中){
in.nextLine();
if(in.nextLine().length()==4){
计数++;
}
}
in.close();
返回计数;
}
公共静态int-SevenCharacters(字符串s2)引发IOException{
扫描仪输入=新扫描仪(新文件(s2));
整数计数=0;
while(在.hasNextLine()中){
in.nextLine();
if(in.nextLine().length()==7){
计数++;
}
}
in.close();
返回计数;
}
公共静态字符串[]Store3Words(字符串s2,整型大小)引发IOException{
//这里是我不断获取ArrayIndexOutOfBoundsException的地方,可能与下面两种方法相同
字符串[]字=新字符串[大小];
字符串temp=“”;
扫描仪输入=新扫描仪(新文件(s2));
int i=0;
while(在.hasNextLine()中){
temp=in.nextLine();
如果(温度长度()==3){
字[i]=temp;
i++;
}
}
in.close();
返回单词;
}
公共静态字符串[]Store4Words(字符串s2,整型大小)引发IOException{
字符串[]字=新字符串[大小];
字符串temp=“”;
扫描仪输入=新扫描仪(新文件(s2));
int i=0;
while(在.hasNextLine()中){
temp=in.nextLine();
如果(临时长度()==4){
字[i]=temp;
i++;
}
}
in.close();
返回单词;
}
公共静态字符串[]Store7Words(字符串s2,整型大小)引发IOException{
字符串[]字=新字符串[大小];
字符串temp=“”;
扫描仪输入=新扫描仪(新文件(s2));
int i=0;
while(在.hasNextLine()中){
temp=in.nextLine();
如果(温度长度()==7){
字[i]=temp;
i++;
}
}
in.close();
返回单词;
}
公共静态字符串[]Char2Dig(字符串[]arr){//||
字符串temp=“”;
字符串q=“”;
字符串str=“”;
对于(int i=0;ipublic 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;
}
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();
}
}