Bassem
Bassem

Reputation: 43

reading text file column by column each in a different array

I have this text file

 SFrm  EFrm   SegAScr Phone
    0    36   -158051 SIL
   37   105   -644247 +NONTRANS+
  106   109    -96452 l SIL w b
  110   112   -125055 w l aa i
  113   115   -150550 aa w 7 i
  116   118   -146662 7 aa i i
  119   122    -46757 i 7 d i
  123   126    -58440 d i SIL e
  127   146    -90776 +MUSIC+
  147   152    -61098 t SIL u b
  153   158    -67393 u t f i
  159   174   -251284 f u f i
  175   178    -79772 f f aa i
  179   194   -134562 aa f 7 i
  195   206    -33695 7 aa a i
  207   223   -194024 a 7 SIL e
  224   350   -434997 +NOISE+
  351   353    -28280 SIL
 Total score:    -2802095

I managed to store this whole thing in a string but I need to store it in some arrays where Each column is represented in a different array I know that I can use .split() as a procedure to convert it into array but I won't be able to discard the spaces between columns.

ps: the text file is generic so these numbers and letters are not constants but the form of it is constant (4 columns)

My main problem now is to catch the duplicate vowels when they are at the beginning of any row in the fourth column and do some calculations with the numbers at the same row if someone has easier approach than mine any help would be appreciated :)

Upvotes: 2

Views: 2841

Answers (1)

Denis
Denis

Reputation: 1229

I hope this help:

    public static void main(String[] args) throws FileNotFoundException, IOException {

          FileReader inputFile = new FileReader("input");

          //Instantiate the BufferedReader Class
          BufferedReader bufferReader = new BufferedReader(inputFile);

          //Variable to hold the one line data
          String line="";int index=0;

          String[] column1= new String[100];
          String[] column2 = new String[100];
          String[] column3=new String[100];
          String[] column4=new String[100];



          while ((line = bufferReader.readLine()) != null){

              String temp="";int count=1;
              column4[index]="";
              //System.out.println(line);
               StringTokenizer st = new StringTokenizer(line," ");
              //String tokenizer gets the token from each space
               while(st.hasMoreTokens())
                 { 

                   temp = st.nextToken();
                    //System.out.println(temp);
                  If(temp.equals("Total")){
                      break;
                  }

                  if(count==1)
                  {
                 // System.out.println(temp);
                  column1[index] = temp;
                  }
                 if(count==2){
                  column2[index] = temp;
                   }   
                 if(count==3)
                {
                  column3[index] = temp;
                 }
                if(count==4)
                {
                column4[index] += temp;
                }
               if(count<4)
                  count++;
                }


             index++;              
           }

          for(int i=0;i<index-1;i++){
              System.out.println(column1[i]+" "+column2[i]+" "+column3[i]+" "+column4[i]);
          }

       }

I declared four arrays to store the columns from above data as you wanted. I am using stringTokenizer to get each token of string. I got this output when I printed the data from above array:

  SFrm EFrm SegAScr Phone
  0 36 -158051 SIL
  37 105 -644247 +NONTRANS+
  106 109 -96452 lSILwb
  110 112 -125055 wlaai
  113 115 -150550 aaw7i
  116 118 -146662 7aaii
  119 122 -46757 i7di
  123 126 -58440 diSILe
  127 146 -90776 +MUSIC+
  147 152 -61098 tSILub
  153 158 -67393 utfi
  159 174 -251284 fufi
  175 178 -79772 ffaai
  179 194 -134562 aaf7i
  195 206 -33695 7aaai
  207 223 -194024 a7SILe
  224 350 -434997 +NOISE+
  351 353 -28280 SIL

Upvotes: 1

Related Questions