for loop - Reading data from RandomAccessFile producing incorrect results - Java -


i have text file 42 lines. each line has more 22,000 numbers separated comma.

i wanted extract numbers each line, , have int array length of 1000 containing 1,000 numbers need each of 42 lines.

for example if array contains 43, 1 , 3244, means want 43th number, 1st number , 3244th numbers each line, starting first line ending 42nd line.

my loop not seem work, reads first line text file has 42 lines of 220000 numbers, , dont know goes wrong.

for(int i=0;i<42;i++){ //irretates through 42 lines of      counter=1; // keep track on line code working     system.out.println("starting line"+i);      st2=new stringtokenizer(raf1.readline(),",");     //raf3 randomaccessfile object containing 42 lines      a:while(st2.hasmoretokens()){         b=is_there(array2,counter);         // is_there method compares rank of taken being read         //the whole array has 1000 numbers want.          if(b==false){              // if rank or order of token [e.g. 1st, 432th] stopping @             //is not among 1000 numbers in array              counter++;                               continue a;         }         else{ //if true             s2=st2.nexttoken();             raf2.writebytes(s2); //write token on new empty text file              raf2.writebytes(","); // follow number comma             counter++;         }     } // end of 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; 

the radiodef answer correct, think there still 1 piece missing. code finds proper numbers, prints them in 1 line, because there no 'next line' statement after loop go through particular line (at least not in code above), example this:

        for(int i=0;i<42;i++){         counter=1; // keep track on token code working         system.out.println("starting line"+i);         st2=new stringtokenizer(raf1.readline(),",");             while(st2.hasmoretokens()){                 boolean b = is_there(array2,counter);                 if(!b){                     st2.nexttoken();                 }else{                     string s2=st2.nexttoken();                     raf2.writebytes(s2 + ",");                 }                 counter++;             }             raf2.writebytes("\r\n");         //next line!         } 

this way, should read, search , print numbers correctly.

what's more, there mistake in comments: counter=1; // keep track on line code working. counter keeps track on token loop working on, not line.

btw. is_there method take shorter form:

public static boolean is_there(int[] x,int y){     for(int : x){         if (i == y) return true;     }     return false; } 

however, not sure, more readable.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -