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

# 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()+"failed.");
 #          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();
     }

}

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

  1. File with extension "doc" is specific to Windows. Why do you want to save data in "doc" and not in simple "txt"? If you still insists on saving in this format you better read Apache POI - Java API To Access Microsoft Format Files
  2. Next time you post some code please use code tags as follows [code] YOUR CODE [/code]

The thing is a still get the same result whether i use .txt or. doc files. I don't think that that is the issue.

The problem is

1. You didn't post your properly formatted code in code tags so it is hard to read your code.
2. You never explained what you want to be output. What is the content that should be in the file? Is it an array of ints, called myInts? Is it a String describing the type of cheeses called stringCheese? What is it and where in your code is it (if you use proper code tags, this is a line number)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.