Advertisement
Guest User

encoded java

a guest
Jan 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class methodGradeLab {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner kb = new Scanner(System.in);
  8. System.out.println("Input: ");
  9. String str = kb.nextLine();
  10.  
  11. String Encoded = "";
  12.  
  13. char ch=0;
  14. int count = 0;
  15. for (int x = 0; x < str.length(); x++) {
  16. if (ch == str.charAt(x)){
  17. count++;
  18. }
  19. else {
  20. Encoded = Encoded + ch;
  21. if(count != 0){
  22. Encoded = Encoded + count;
  23. }
  24. ch = str.charAt(x);
  25. count = 1;
  26. }
  27. }
  28. Encoded = Encoded + ch;
  29. if(count != 1){
  30. Encoded = Encoded + count;
  31. }
  32. System.out.println("Encoded: " + Encoded);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement