Reading data from RandomAccessFile generating incorrect results - Java

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; 
+7
java for-loop stringtokenizer randomaccessfile
source share
2 answers

Radiodef's answer is correct, but I think one part is still left. The code finds the correct numbers, but prints them on one line, because after a loop that goes through a certain line (at least not in the code above), there is no β€œnext line” instruction, for example:

  for(int i=0;i<42;i++){ counter=1; // to keep track about on which TOKEN the code is 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! } 

Thus, he must correctly read, search, and print numbers.

What else, there is an error in the comments: counter=1; // to keep track about on which line the code is working counter=1; // to keep track about on which line the code is working . counter keeps track of which marker the loop is running on, and not the line.

BTW. The is_there method is_there also have a shorter form:

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

However, I'm not sure this is more readable.

+5
source share

You have one problem: when you find an index that is not in your array, you are not actually missing a token:

 if ( b == false ) { // don't actually skip the token !! counter++; continue a; } else { s2 = st2.nextToken(); raf2.writeBytes(s2); raf2.writeBytes(","); counter++; } 

This means that your StringTokenizer gets 1 token every time you try to skip.

This can lead to an infinite loop, for example.

 if ( b == false ) { // so skip the token !! st2.nextToken(); counter++; continue a; } else { s2 = st2.nextToken(); raf2.writeBytes(s2); raf2.writeBytes(","); counter++; } 

As a note, the loop can be rewritten more elegantly as follows:

 while (st2.hasMoreTokens()) { s2 = st2.nextToken(); if (is_there(array2, counter)) { raf2.writeBytes(s2); raf2.writeBytes(","); } ++counter; } 

You should also:

  • use more descriptive names for things.
  • declare variables in the area in which they are used .
+7
source share

All Articles