3

My question is how would I write a simple compiler ,that is like the compilers used in fax machines, that would convert something like aaaavvvvvddddddddddd to 4a5vBd.

Also, I get to "Assume" that any string entered will not contain uppercase letters and no numbers, and that any string will contain less than 61 of any type of character so, I get to assume no one will put in 64 continues a's in my program.

This is as far as I gotten

import java.util.*;
public class Program4
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int n;
        char cn;
        String word;
        String numChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        System.out.println("Hello, please enter a string");
        word = scan.nextln();

        if(n <= 61)
        {
        int n = ?;
        cn = numChars.charAt(n);
        }
    }
}

I assume I need to use a loop, but I don't know what I should use to count the repeating letters and then tell how many letters of that type are in a row. Now I am only asking for advice and not so much for code, because I want to do it but, as a beginner my Java "Vocabulary" isn't very big right now.

Any advice/ tips would be greatly appreciated.

Sincerely, Mr.Trips

Well I am back and it appears my code here likes to only print out 147. No matter what I type in I will always get 147. I have tried to hand trace all my variables, but when I do it I get exactly what I want, and I must have some error in my logic. Any thoughts?

import java.util.*;    
public class Program4
{
    public static void main(String[] args)
    {

        Scanner scan = new Scanner(System.in);
        int n = 0;
        int s = 0;
        char a;
        char b;
        char c;
        String word;
        String numChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        System.out.println("Please enter a string");
        word = scan.nextLine();


        while (n < word.length()) 
        {
            a = word.charAt(n);
            b = a;
            n = n ++;
            a = word.charAt(n);

            if (a == b)
            {
                    s = (s + 1) ; 
            }
            else if (a != b);
            {
                c = numChars.charAt(s);
                System.out.print(b + c);
                s = 0;
                c = 0;
                break;

            }
        }
    }
}

Thank you again!

Mr.Trips
  • 39
  • 3
  • @brso05 gave you a decent answer, but you also asked for tips, so here's mine: you might want to consider what you're going to do with the compressed string. If you ever are going to uncompress it in any way (and you'll need to in order to write tests for your compression, among other things), you'll have to think about how you'll handle digits and upper case characters in the incoming string. What if you receive "`4a5vBd`" as your input? You won't change it, but when you uncompress it, it would make "`aaaavvvvvddddddddddd`", which isn't the original input. – CPerkins Sep 23 '14 at 20:19
  • Oh, and you also should think about how you're going to handle more repetitions than fit in your current schema (how will you encode "`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`")? – CPerkins Sep 23 '14 at 20:20
  • 1
    Thank you! Especially for the food for thought! But I would like to apologize for not adding more parameters to my post. I get to "Assume" that they will add "nice" strings that do not include numbers or upper case letters, and that any string of letters will be less than 61 to ensure that the code works, assuming I code it right. – Mr.Trips Sep 23 '14 at 20:39

1 Answers1

3

Since you don't want code this is logically how to do it. You are right you should loop through the string for each char. Store the last char in a variable and keep a counter variable. Compare current char to last char if it is equal then increment the counter. As soon as it is not equal to the last char then add counter + last char to result string and reset counter variable. Each iteration update last char variable.

brso05
  • 13,142
  • 2
  • 21
  • 40