Trying to write a simple compiler in java (I am using notepad ++)

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

Also, I get "Assume" that any line entered will not contain any uppercase letters and numbers, and any line will contain less than 61 characters of any type, so I am assuming that no one will be put into 64 continues in my program.

This is as far as I got

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'm guessing I need to use a loop, but I don't know what I should use to count the repeated letters and then indicate how many letters of that type are in the string. Now I only ask for advice and not so much for the code because I want to do this, but as a beginner my Java "Dictionary" is not very big right now.

Any advice / advice is greatly appreciated.

Regards, Mr. Trips

Well i'm back and i feel like my code here only likes to print 147. no matter what i type i always get 147. i tried to track all my variables but when i do 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;

            }
        }
    }
}

      

Thanks again!

+3


source to share


1 answer


Since you don't want the code, it's logical how to do it. You are correct, you have to loop through the line for each char. Store the last char in a variable and store the counter variable. Compare the current char to the last char if equal, then increment the counter. Once it doesn't equal the last char, add counter + last char to the result string and reset the counter variable. Each iteration updates the last char variable.



+3


source







All Articles