0

我正在研究netbeans...我需要读取一个文件并进行标记,然后将其存储在一个数组中以供我将来的操作使用...我已经附加了第5行包含标记的代码...同时转换为数组iam得到错误为

例外 :

" Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1000
at preprocess.mainpage.jButton2ActionPerformed(mainpage.java:224)
at preprocess.mainpage.access$100(mainpage.java:18)
at preprocess.mainpage$2.actionPerformed(mainpage.java:62)"

代码:

int counter=-1;
           int n=0;
           String[] arr = new String[1000];
try
{

    BufferedReader b = new BufferedReader(new FileReader("C:/Users/sky/Documents/NetBeansProjects/Preprocess/src/preprocess/cdr1.txt"));
                    String line;
                    while ((line = b.readLine()) != null)
                    {
                       counter+=1;
                       StringTokenizer st2 = new StringTokenizer(line, " ");
                       String line5 = (String) st2.nextElement();
                       arr[n] = line5;
                       n++;
                    }
}
catch (Exception e)
{
}
4

1 回答 1

0

ArrayIndexOutOfBoundsException因为可能是您的数组大小较小。所以更好地使用ArrayList如下:

int counter=-1;
int n=0;
//String[] arr = new String[1000];
List<String> list = new ArrayList<String>(); // Create ArrayList
try{    
    BufferedReader b = new BufferedReader(new FileReader("C:/Users/sky/Documents/NetBeansProjects/Preprocess/src/preprocess/cdr1.txt"));
    String line;
    while ((line = b.readLine()) != null) {
         counter+=1;
         StringTokenizer st2 = new StringTokenizer(line, " ");
         String line5 = (String) st2.nextElement();
         //arr[n] = line5;
         //n++;
         list.add(line5); // Add you string into list
   }
 String[] aa = list.toArray(new String[0]); // convert list into String of array if you need it
}
catch (Exception e){
}
于 2013-11-06T05:15:04.870 回答