Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RunLengthEncoding {
  4.  
  5.     public static void main(String[] args) {
  6.    
  7.         int counter = 1;
  8.         String last = "";
  9.         String output = "";
  10.         String addToOutput = "";
  11.        
  12.        
  13.         Scanner s = new Scanner(System.in);
  14.        
  15.         System.out.print("Enter input string: ");
  16.         String inputstring = s.nextLine();
  17.        
  18.        
  19.         System.out.print("Enter flag character: ");
  20.         String inputflag = s.nextLine();
  21. //      char inputflag = s.nextLine().charAt(0);
  22. //      if(inputflag < '#' || (inputflag > '&' && inputflag != '*') || inputflag == '%'){
  23. //          System.out.print("Bad Input.");
  24. //          return;
  25.         }
  26.         try{
  27.         if(inputstring != "#" || inputstring != "$" || inputstring != "&" || inputstring != "*"){
  28.             throw new IllegalArgumentException("Bad Input.");
  29.    
  30.        
  31.         }
  32.         else{
  33.             System.out.print(output);
  34.         }
  35.  
  36.         for (int i = 0; i<inputstring.length(); i++){
  37.            
  38.                
  39.            
  40.                 if((""+inputstring.charAt(i)).equals(last)){
  41.                     counter ++;
  42.         }
  43.             else{
  44.                 if(counter>3)
  45.                 addToOutput = inputflag + last + counter;
  46.                 else{
  47.                     addToOutput = "";
  48.                     for(int j =0; j<counter; j++){
  49.                         addToOutput = addToOutput + last;
  50.                     }
  51.                    
  52.                 }
  53.                 output = output + addToOutput;
  54.             counter = 1;
  55.            
  56.         }
  57.                
  58.                
  59.         last = "" + inputstring.charAt(i);
  60.        
  61.                
  62.                 }
  63.            
  64.        
  65.         catch(IllegalArgumentException e){
  66.             System.out.println(e.getMessage());
  67.         }
  68.        
  69.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement