Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RunLengthEncoding {
- public static void main(String[] args) {
- int counter = 1;
- String last = "";
- String output = "";
- String addToOutput = "";
- Scanner s = new Scanner(System.in);
- System.out.print("Enter input string: ");
- String inputstring = s.nextLine();
- System.out.print("Enter flag character: ");
- String inputflag = s.nextLine();
- // char inputflag = s.nextLine().charAt(0);
- // if(inputflag < '#' || (inputflag > '&' && inputflag != '*') || inputflag == '%'){
- // System.out.print("Bad Input.");
- // return;
- }
- try{
- if(inputstring != "#" || inputstring != "$" || inputstring != "&" || inputstring != "*"){
- throw new IllegalArgumentException("Bad Input.");
- }
- else{
- System.out.print(output);
- }
- for (int i = 0; i<inputstring.length(); i++){
- if((""+inputstring.charAt(i)).equals(last)){
- counter ++;
- }
- else{
- if(counter>3)
- addToOutput = inputflag + last + counter;
- else{
- addToOutput = "";
- for(int j =0; j<counter; j++){
- addToOutput = addToOutput + last;
- }
- }
- output = output + addToOutput;
- counter = 1;
- }
- last = "" + inputstring.charAt(i);
- }
- catch(IllegalArgumentException e){
- System.out.println(e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement