我有一个文本文件,共有42行。每行都有超过22,000个用逗号分隔的数字。
我想从每行中提取特定的数字,并且我有一个长度为1000的整数数组,其中包含我需要从这42行中获取的1,000个数字。例如,如果该数组包含43、1、3244,则意味着我要从第一行到第42行的每一行中获取第43个数字、第1个数字和第3244个数字。
我的for循环似乎不起作用,它只读取了文本文件中的第一行,该文件共有42行、220000个数字,我不知道出了什么问题。
我想从每行中提取特定的数字,并且我有一个长度为1000的整数数组,其中包含我需要从这42行中获取的1,000个数字。例如,如果该数组包含43、1、3244,则意味着我要从第一行到第42行的每一行中获取第43个数字、第1个数字和第3244个数字。
我的for循环似乎不起作用,它只读取了文本文件中的第一行,该文件共有42行、220000个数字,我不知道出了什么问题。
for(int i=0;i<42;i++){ //irretates through the 42 lines of
counter=1; // to keep track about on which line the code is working
System.out.println("Starting line"+i);
st2=new StringTokenizer(raf1.readLine(),",");
//raf3 is a RandomAccessFile object containing the 42 lines
a:while(st2.hasMoreTokens()){
b=is_there(array2,counter);
// is_there is a method that compares the rank of the taken being read with
//the whole array that has the 1000 numbers that i want.
if(b==false){
// if the rank or order of token [e.g. 1st, 432th] we are stopping at
//is not among the 1000 numbers in the array
counter++;
continue a;
}
else{ //if true
s2=st2.nextToken();
raf2.writeBytes(s2); //write that token on a new empty text file
raf2.writeBytes(","); // follow the number with a comma
counter++;
}
} // end of for loop
public static boolean is_there(int[] x,int y){
boolean b=false;
for(int i=0;i<x.length;i++){
if(x[i]==y) {b=true; break;}
}
return b;
readLine()
返回null来表示。 - user207421