Anupznk

Untitled

Mar 24th, 2021
39
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class NonRepeatingCharacterFinder {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. MyQueue <Character> queue = new MyQueue<>();
  8.  
  9. Scanner input = new Scanner(System.in);
  10. String inputStr = "";
  11. System.out.print("String_old = ");
  12. inputStr = input.next();
  13.  
  14. char[] inputCharSequence = inputStr.toCharArray();
  15. StringBuffer outputStr = new StringBuffer();
  16.  
  17. for (int i = 0; i < inputCharSequence.length; i++) {
  18. char currCharacter = inputCharSequence[i];
  19.  
  20. char topOfQueue = 0;
  21. if (!queue.isEmpty()) {
  22. try {
  23. topOfQueue = queue.top();
  24. } catch (Exception e) {
  25. System.out.println(e.toString());
  26. }
  27. }
  28. if (i == 0 || topOfQueue != currCharacter) {
  29. queue.enqueue(currCharacter);
  30. try {
  31. outputStr.append(queue.top());
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35.  
  36. } else {
  37. try {
  38. queue.dequeue();
  39. } catch (Exception e) {
  40. System.out.println(e.toString());
  41. }
  42. if (!queue.isEmpty()) {
  43. try {
  44. outputStr.append(queue.top());
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. } else {
  49. outputStr.append("#");
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. System.out.println("String_new = " + outputStr);
  57.  
  58.  
  59. }
  60. }
  61.  
RAW Paste Data