• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can Someone tell me whats wrong with this RLE simple code???  RSS feed

 
moaly LeGrand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone...i wrote this code that implements the RLE (Run Length Encoder) technique that is supposed to accept a tring from a file "aly.txt"(in my example) the file contains any streams like (ex: aabbbcccdff)and the output should be (a,2)(b,3)(c,3)(d,1)(f,2) ..it accumulates the repeated character as a manner of encoding...so can u plz figure out why the counter is not working..??
Code:
import java.io.*;
import java.math.*;
class Encoder{
public static void main (String args[]) throws IOException
{
int ch[];
ch=new int[100];
int i,ctr,j;

File inFile=new File ("aly.txt");
File outFile=new File ("aly2.txt");
DataInputStream in =new DataInputStream(new FileInputStream (inFile));
PrintStream out=new PrintStream(new FileOutputStream (outFile));



i=0;j=0;ctr=0;
i=in.read();
ch[j]=i;


while((in.read())!=-1)
{

j++;
i=in.read();
ch[j]=i;



if (ch[j]!=ch[j-1])
{
out.write(40);
out.write(ch[j-1]);
out.write(44);
out.print(j-1); /*<---here is the part for the counter that is not workin'(it gives wrong count)*/
out.write(41);
ch[0]=ch[j];
j=0;
}

}

in.close();
out.close();
}

}


Plz help me out asap ...thnxxxx in advanceeee
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Plz help me out asap ...thnxxxx in advanceeee


Please avoid using SMS or "1337" spellings and abbreviations. It makes things difficult on our members to whom English is a second language. Also, we are all volunteers here answering questions in our spare time. Telling us to help out "asap" is a good way to get your question ignored.


so can u plz figure out why the counter is not working..??

While it would be simple for us to point to your problem, that really wouldn't help you learn how to fix your own problems. The first thing you should do is get away from the computer. Sit in a quiet corner and write, in plain English, what you want your code to accomplish line by line. I'd even suggest tossing this code away and starting fresh. Take your plain English code and implement it one loop at a time. First, try to read the file in. Next, try to get a count of the letter 'a' and so on. Trying to get your code right on the first try is a receipe for failure.
Something you should always do is sprinkle System.out.println() statements throughout your code. Print out the value of every variable every time you change it. You WILL be surprized. You can always comment them out once you no longer need them. Don't bother deleting them, you'll need them again.
Last, a read through of the Java Tutorial chapter on IO is a good place to gain some understanding on how to use java.io effectively (i.e. Streams are for binary data, Readers/Writers are for text).
 
moaly LeGrand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...sorry for that "asap" thingy...and execuse me because i am new here and new to the language as well...i really appreciate ur cooperation..you are my new family you know :roll:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!