Results 1 to 2 of 2
  1. #1
    puk284 is offline Member
    Join Date
    Apr 2009
    Posts
    16
    Rep Power
    0

    Default Reading data to file

    Hi,

    The problem i'm having is that the data that i want to appear on the file doesnt apear fully. I want the full results of the program to be saved to a doc file but i only get the next number in the sequence. Here is my code

    <code>
    # import java.io.*;

    # public class identify2 {
    # int N=1;
    # char[] BitStream=new char[569878];
    # double[] Entropy=new double[30];
    # int MaxLength=0;
    # boolean EOF;

    // read data from a text file
    # void ReadData()
    # {
    # System.out.println("after here would be a read data");


    # try
    # {
    # String fileName="f:\\malay-binary3.txt";

    # File input=new File(fileName);

    # PushbackReader in=new PushbackReader(new BufferedReader(new FileReader(input)));

    #int c = 1;

    # int i = 0;
    # while(c!=-1)
    # {String number="";

    # c = in.read();
    # MaxLength+=1;

    # if(c==-1)
    # break;
    # else
    # in.unread(c);


    # BitStream[i]=(char)(in.read());

    # i++;

    # }//end of try

    # }
    # catch(EOFException e)
    # {EOF=true;}

    # catch(FileNotFoundException e)
    # {System.err.println(e);
    # return;
    # }
    # catch(IOException e)
    # {System.err.println("Err reading input file"+e);
    # return;
    }
    # } //end of ReadData

    //analyize the data (chunk and calculate the entropy )
    # void processfile()
    # {
    # int Find = 0,f = 0,f1 = 0,p,N_entropy,L,Length = 0, index = 0,end_index=0;
    # double Probability, Local_Entropy = 0.0;

    # int Frequency[] = new int[MaxLength];
    # String Patterns[] = new String[MaxLength];
    # String Pattern = "";

    # Length = MaxLength;

    # for( N = 1; N <=3; N++ )
    # {
    # System.out.println("When N=" + N);

    // for every fixed length N has N entropy
    # for ( N_entropy=0;N_entropy< N ;N_entropy++ )
    # {
    # L = Length - N_entropy;
    # index = N_entropy;
    # end_index = index + (L / N -1) * N ;

    # for ( index = N_entropy; index<=end_index; index+=N )
    # {
    # for ( p= index; p < index+N; p++ )
    # Pattern += BitStream[p];

    # for (int m=0; m<f; m++ )
    # {
    # if( Patterns[m].equals(Pattern) && (Find==0) )
    # { Find = 1;
    # f1 = m;
    }
    # }

    # if (Find == 1)
    # { Frequency[f1] += 1;

    # Pattern = "";
    # Find = 0;
    # }
    # else
    # {
    # Patterns[f] = Pattern;

    # Frequency[f] += 1;

    # f +=1;
    # Pattern = "";}

    } //end of for index


    # f=0;
    # int Patterns_N = L/N; //how many patterns of N length contains in the BitStream

    # do
    { Probability = (double)(Frequency[f])/(double)(Patterns_N); //Probability of Patterns

    # Local_Entropy += Probability * ( Math.log(Probability)/Math.log(2));

    # f+=1;
    }
    # while (Frequency[f]!=0);

    # Entropy[N_entropy] = (-Local_Entropy);
    # System.out.println("Entropy(" + N_entropy + ")=" + Entropy[N_entropy]);

    // initialize the variables
    # f=0;
    # for (int i=0;i<MaxLength;i++)
    # {Frequency[i] =0;
    # Patterns[i] = "";}


    # Pattern="";
    # f=0;f1=0;
    # Find=0;
    # Local_Entropy=0;
    # p=0;

    # }
    # System.out.println();

    # }//end of for "N"

    # }

    //output the result to a text file
    # void writeData()
    # {

    # try
    # {
    # String fileName="f:\\test86.doc";
    # File output=new File(fileName);
    # output.createNewFile();

    # if(!output.isFile())
    # {System.out.println("Creating"output.getPath()"fai led.");
    # return;
    # }

    # BufferedWriter out=new BufferedWriter(new FileWriter(output.getPath(),true));
    # out.write("Then entropy of length " + N + ":" + "\n");
    # for( int i = 0; i < N; i++ )
    # {
    # //System.out.println("En[i]="+Entropy[i]);
    # out.write(Double.toString( Entropy[i] ) + "\n");

    # }
    # out.write( "\n" );
    # out.close();

    # }//end of try

    # catch(IOException e)
    # {System.out.println("Error writing the file"+e);}
    }// end of writeData

    //main class

    # public static void main(String[] args)
    # {
    # identify2 test=new identify2();

    # test.ReadData();
    # test.processfile();
    # test.writeData();
    }

    }

    <code>
    it should give me the following when it is saved to the doc file
    after here would be a read data
    When N=1
    Entropy(0)=1.0000741842259357

    When N=2
    Entropy(0)=1.9998076934003925
    Entropy(1)=1.99996372281124

    When N=3
    Entropy(0)=2.994268823400294
    Entropy(1)=2.993906140011204
    Entropy(2)=2.9948443499810233

    but it gives me
    Then entropy of length 4:
    2.994268823400294
    2.993906140011204
    2.9948443499810233
    0.0

    Any help would be greatly apperciated. Thank you for your help in advance
    Last edited by puk284; 04-28-2009 at 10:42 PM. Reason: more clarity

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Use the code tags

    Please edit your post and place your code between code tags (you can use the "#" in the above tool bar). It will make your code a lot more readable.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 07:29 PM
  2. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  3. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 10:10 AM
  4. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 AM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •