HELP: Java code to compress 0s using run-length compression technique
Hello there, Can you help me guys to write a java program which inputs a string of 1s and 0s and compresses the 0s using the run-length compression technique
For example, the users enters this string:
000000000001000011000101
is replaced with just the number of 0s sequences without the number of 1s like this:
11 4 0 3 1
and then replace each decimal value of the above with a 4-bit binary value (nibble). Note: If you need to code a value larger than 15, you need to use two consecutive 4-bit nibbles.
for example for number 11 the binary value of it should be like this: 1011 0000
number 0 it will be like this: 0000 0000
for number 15 it will be like this: 1111 0000
Can someone help me with the Java codes?
Re: HELP: Java code to compress 0s using run-length compression technique
What have you done so far and where are you stuck?
Please ensure any code you post is in [code] tags [/code].
Also ensure you supply any compiler errors in full, highlighting the line they occur on.
If it runs then provide any exceptions that are thrown, including stack traces, also highlighting the line they are thrown from.
If it runs without exception, then you need to show what you rinput was and what result you saw and what result you expected to see.
Re: HELP: Java code to compress 0s using run-length compression technique
Honestly, I just have basic Java background. So I would appreciate helping me writing the code.
I have outlined what my code should look like in natural language and here is what I got.
To output the encoded sequence as decimals, I would say something like this:
(Iterate over) every character in the string. For each character do the following:
If it is a 0 we add 1 to the number of 0s currently read
If it is a 1, we:
append the number of currently read 0s to the output string
append 1 to the output string
set the number of currently read 0s to 0
If the current character is the last one, we append the number of 0s currently read, but only if we have read more than one 0
When we are done, we output the output-string
That was for the first part. I couldn't get the second part which is for replacing the decimal values to binary bits.
Please help me with codes if you can.
Thanks in advance ...
Re: HELP: Java code to compress 0s using run-length compression technique
So start from the beginning.
You need some code to prompt the user for a binary String (ie 0's and 1's).
Once you have that working then you need a loop over the String and start on your logic above.
You don't need anyone here to do that as you've already done the hard part of coming up with a plan.
Re: HELP: Java code to compress 0s using run-length compression technique
Actually somebody helped me doing that part. So, I just need somebody to help me with the Java code itself. Is there anybody that starts with me from scratch? I don't think it will be a long code.
I would appreciate that.
Thanks,
1 Attachment(s)
Re: HELP: Java code to compress 0s using run-length compression technique
Quote:
You need some code to prompt the user for a binary String (ie 0's and 1's). --tolls
Quote:
Actually somebody helped me doing that part. So, I just need somebody to help me with the Java code itself. --salamjamal10
Ah.... what part of user input isn't code? If you have done the user input portions already, go ahead and post them here and we can help you move forward (when you as a specific question of course). Your questions so far have been the rough equivalent of "How do I program games?".
Attachment 4223
Re: HELP: Java code to compress 0s using run-length compression technique
Quote:
Originally Posted by
salamjamal10
Actually somebody helped me doing that part. So, I just need somebody to help me with the Java code itself. Is there anybody that starts with me from scratch? I don't think it will be a long code.
I would appreciate that.
Thanks,
Or, to put it another way, we are not a code factory.
Please see my sig.
You should be able to write the first steps I mentioned above, at the very least the user input.
If you cannot then I suggest going back through your notes.