| | |
Reading data to a file
![]() |
•
•
Join Date: May 2008
Posts: 22
Reputation:
Solved Threads: 0
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()+"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();
}
}
<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
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()+"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();
}
}
<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 puk; Apr 28th, 2009 at 10:43 am. Reason: more clarity
- 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
- Next time you post some code please use code tags as follows [code]YOUR CODE[/code]
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Sep 2008
Posts: 1,532
Reputation:
Solved Threads: 191
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)?
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)?
Last edited by BestJewSinceJC; Apr 29th, 2009 at 2:11 am.
![]() |
Similar Threads
- Help reading data from a file (C++)
- Help with reading data from a file (C++)
- reading data from a file into vectors (C++)
- Reading data file in C (C)
- No documentation on reading in int from file (Python)
- read data file in C (C)
- reading data from file to vector (C++)
- Reading from external data file (C++)
Other Threads in the Java Forum
- Previous Thread: Quicksort median of three pivot help
- Next Thread: Image creation
| Thread Tools | Search this Thread |
6 @param actuate affinetransform android api applet application arc array automation balls binary bluetooth bold business c++ class client code codesnippet collections color compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide ideas image ingres intersect invokingapacheantprogrammatically j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list map method methods mobile mysql netbeans nextline object parameter php pong problem producer program project projectideas recursive replaysolutions rim scanner sell server set size sms sql sun swing swt terminal threads tree web websites windows






