Results 1 to 3 of 3
Thread: Run Length Encoding program
- 03-09-2012, 06:23 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Run Length Encoding program
Hey guys,
So I'm working on a program which asks the user for a string of input (can be characters and numbers), and encodes the string like this:
Input: AAAABBBCD1223
Delimiter: *
Encodes: *4ABBBCD1223
If there are 3 or less of the same letters in a row, don't encode it. Don't encode numbers either. I don't know how or where to check the string that condition. Should I make another if statement or add it in an existing one?
Also, I have 2 errors that I don't know how to fix. I've tried indexOf, typingcasting it to char, nothing seems to work, but it seems like charAt makes the most sense here.
RunLengthEncoding.java:53: error: incompatible types
decoded = RLECode.charAt(i);
^
required: String
found: char
RunLengthEncoding.java:71: error: incompatible types
decoded = RLECode.charAt(i);
^
required: String
found: char
That's the first part of the program, ignore the printPicture method.
Here's my code:
Java Code:import java.util.Scanner; public class RunLengthEncoding { public static void main (String[] args) { int w = 0; String runLengthCode; String decoded; String delim; Scanner input = new Scanner(System.in); System.out.print("Enter the width: "); if (input.hasNextInt()) { w = input.nextInt(); System.out.print("Enter the run-length code: "); runLengthCode = input.next(); System.out.print("Enter a single character: "); delim = input.next(); if (delim.length() == 1) { decoded = decodedRLE(runLengthCode, delim); printPicture(decoded, w); } else System.out.println("Invalid input. Only one character is allowed."); } else System.out.println("Invalid input. Only numbers are allowed."); } private static String decodedRLE(String RLECode, String delimiter) { int i = 0; int count = 1; String decoded = ""; for (i = 0; i < RLECode.length();) { if(i + 1 < RLECode.length() && RLECode.charAt(i) == RLECode.charAt(i + 1)) { if (RLECode.charAt(i) <= '9' && RLECode.charAt(i) >= '0') { count = 1; decoded = RLECode.charAt(i); i++; } else { while(i + 1 < RLECode.length() && RLECode.charAt(i) == RLECode.charAt(i + 1)) { count++; i++; } decoded = delimiter + count + RLECode.charAt(i); i++; count = 1; } } else { count = 1; decoded = RLECode.charAt(i); i++; } } return decoded; } private static void printPicture(String decodedRLECode, int width) { System.out.print(decodedRLECode + " " + width); } }
- 03-09-2012, 04:21 PM #2
Member
- Join Date
- Mar 2012
- Location
- Hyderabad, India
- Posts
- 8
- Rep Power
- 0
Re: Run Length Encoding program
Hi,
try this
decoded = Character.toString(RLECode.charAt(i));
seems to be a working :) .
Regards
- 03-09-2012, 04:30 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Re: Run Length Encoding program
error: incompatible types
Similar Threads
-
Url encoding
By RaviGupta0709 in forum Web FrameworksReplies: 0Last Post: 08-23-2011, 09:02 AM -
Difference between length() and length
By mitra in forum New To JavaReplies: 7Last Post: 07-28-2011, 01:46 AM -
Java UTF-16 Encoding
By skiforfun in forum Advanced JavaReplies: 4Last Post: 05-04-2011, 07:40 PM -
Problem with Encoding
By agajantorayev in forum Advanced JavaReplies: 7Last Post: 02-28-2011, 08:47 PM -
Some help with encoding...
By nm123 in forum NetworkingReplies: 0Last Post: 04-15-2008, 01:22 AM
Bookmarks