Closed. This question needs debugging details。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
这是我的指示。我给了2个txt文件,一个电话号码和一个英语单词。假设数字2到9分别代表3到4个字母:
我被要求找到每个电话号码的前3个数字,后4个数字以及所有7个数字的对应单词。样本输出:
我的代码可以编译并在最后4位和所有7位上正常运行。但是,它返回
当我包括
我知道它试图告诉我某个数组超出某个范围,
但我很难找出答案。
如果您没有收到答复,请先谢谢您。 :D
您可以尝试添加以下语句:
如果在程序崩溃之前,您将看到:
“当前数字为12”,那么您会知道出了什么问题,因为“ 12” .charAt(2)将给您带来超出范围的错误。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
这是我的指示。我给了2个txt文件,一个电话号码和一个英语单词。假设数字2到9分别代表3到4个字母:
2 (A B C)
3 (D E F)
4 (G H I)
5 (J K L)
6 (M N O)
7 (P Q R S)
8 (T U V)
9 W X Y Z
我被要求找到每个电话号码的前3个数字,后4个数字以及所有7个数字的对应单词。样本输出:
2475463 : AIRLINE
247 : AHR
247 : AHS
247 : AIR
247 : AIS
247 : BIR
247 : BIS
5463 : KIME
5463 : KIND
5463 : LIME
5463 : LIND
5463 : LINE
我的代码可以编译并在最后4位和所有7位上正常运行。但是,它返回
TEL: 2475463
2475463 : AIRLINE
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:658)
at Lab11.match(Lab11.java:60)
at Lab11.main(Lab11.java:46)
当我包括
match(first3[j],listOf3,representation)
时。我知道它试图告诉我某个数组超出某个范围,
但我很难找出答案。
如果您没有收到答复,请先谢谢您。 :D
import java.util.*;
import java.io.*;
public class Lab11{
public static void main(String[] args)
throws FileNotFoundException{
//First scan to determine # of phone nums
File nums = new File("telephone.txt");
int size = arrSize(nums,7,"604 ");
//Copy nums to array
//Eliminating 604 and space
String[] phoneNums = copy(nums,size,7,"604 ");
String[] first3 = new String[size];
String[] last4 = new String[size];
for(int j=0;j<first3.length;j++){
first3[j] = phoneNums[j].substring(0,2);
}
for(int j=0;j<last4.length;j++){
last4[j] = phoneNums[j].substring(3,7);
}
//Scan to get the # of words consisting of 7/3/4 letters
//Eliminating space and apostrophe
File words = new File("word_list.txt");
int wordWidth7 = arrSize(words,7);
int wordWidth3 = arrSize(words,3);
int wordwidth4 = arrSize(words,4);
//Copy these words to array
//Eliminating space and apostrophe
String[] wordList = copy(words,wordWidth7,7);
String[] listOf3 = copy(words,wordWidth3,3);
String[] listOf4 = copy(words,wordwidth4,4);
char[][] representation = {{'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'}};
for(int j=0;j<phoneNums.length;j++){
System.out.println("TEL: "+phoneNums[j]);
match(phoneNums[j],wordList,representation);
match(first3[j],listOf3,representation);
match(last4[j],listOf4,representation);
System.out.println("--------");
}
}
public static void match(String num,String[] words,char[][] rep){
for(int i=0;i<words.length;i++){
int matchFound=0;
for(int j=0;j<words[i].length();j++){
for(int column=0;column<4;column++){
if(words[i].charAt(j)==rep[num.charAt(j)-48-2][column])
matchFound++;
}
}
if(matchFound==num.length())
System.out.println(num+" : "+words[i]);
}
}
public static int arrSize(File f, int length, String delimiter)
throws FileNotFoundException{
int arrSize = 0;
Scanner in = new Scanner(f).useDelimiter(delimiter);
while(in.hasNext()){
String temp = in.next().trim();
if(temp.length()==length){
arrSize++;
}
}
in.close();
return arrSize;
}
public static int arrSize(File f, int length)
throws FileNotFoundException{
int arrSize = 0;
Scanner in = new Scanner(f);
while(in.hasNext()){
String temp = in.next().trim().replace("'","");
if(temp.length()==length){
arrSize++;
}
}
in.close();
return arrSize;
}
public static String[] copy(File f,int size,int length,String delimiter)
throws FileNotFoundException{
Scanner in = new Scanner(f).useDelimiter(delimiter);
String[] list = new String[size];
int i = 0;
while(in.hasNext()){
String tmp = in.next().trim();
if(tmp.length()==length){
list[i] = tmp;
i++;
}
}
in.close();
return list;
}
public static String[] copy(File f,int size,int length)
throws FileNotFoundException{
Scanner in = new Scanner(f);
String[] list = new String[size];
int i = 0;
while(in.hasNext()){
String tmp = in.next().trim().replace("'","");
if(tmp.length()==length){
list[i] = tmp;
i++;
}
}
in.close();
return list;
}
}
最佳答案
您可以尝试添加一些打印语句以帮助您调试。例如,在引起问题的那一行之前:
if(words[i].charAt(j)==rep[num.charAt(j)-48-2][column])
您可以尝试添加以下语句:
System.out.println("The current num is: " + num);
如果在程序崩溃之前,您将看到:
“当前数字为12”,那么您会知道出了什么问题,因为“ 12” .charAt(2)将给您带来超出范围的错误。
关于java - 字符串索引超出范围,但是不知道:( ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25006359/