Results 1 to 3 of 3
  1. #1
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    skynet357 is offline Member
    Join Date
    Mar 2012
    Location
    Hyderabad, India
    Posts
    8
    Rep Power
    0

    Default Re: Run Length Encoding program

    Hi,
    try this

    decoded = Character.toString(RLECode.charAt(i));

    seems to be a working :) .

    Regards

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Url encoding
    By RaviGupta0709 in forum Web Frameworks
    Replies: 0
    Last Post: 08-23-2011, 08:02 AM
  2. Difference between length() and length
    By mitra in forum New To Java
    Replies: 7
    Last Post: 07-28-2011, 12:46 AM
  3. Java UTF-16 Encoding
    By skiforfun in forum Advanced Java
    Replies: 4
    Last Post: 05-04-2011, 06:40 PM
  4. Problem with Encoding
    By agajantorayev in forum Advanced Java
    Replies: 7
    Last Post: 02-28-2011, 07:47 PM
  5. Some help with encoding...
    By nm123 in forum Networking
    Replies: 0
    Last Post: 04-15-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •