I have a text file with 42 lines. Each line contains more than 22,000 numbers, separated by a comma.
I wanted to extract specific numbers from each row, and I have an int array of length 1000 containing 1000 numbers that I need from each of these 42 rows.
For example, if the array contains 43, 1, 3244, this means that I want the 43rd number, 1st number and 3244th number from each row, starting from the first row ending with the 42nd row.
My for loop doesn't work, it only reads the first line from a text file, which has 42 lines of 220,000 numbers, and I don't know where this happens.
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 [eg 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;
java for-loop stringtokenizer randomaccessfile
Tagwa warrag
source share