Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package org.softuni.whileloop.exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StreamOfCharacters {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. // these will keep track of whether we have encountered the "magic" chars
  10. boolean c = false;
  11. boolean o = false;
  12. boolean n = false;
  13.  
  14. String nextWord = "";
  15.  
  16. String nextLine = scanner.nextLine();
  17. while (!"End".equals(nextLine)) {
  18. // we will be reading lines until the "End" command is entered
  19.  
  20. char nextChar = nextLine.charAt(0);
  21. // the input is converted to char by taking the first letter of the input string
  22. // in this case the input only has one letter, so we take it
  23.  
  24. if ((nextChar >= 'A' && nextChar <= 'Z') || (nextChar >= 'a' && nextChar <= 'z')) {
  25. // this is how we check if nextChar is a latin letter
  26. if (Character.isAlphabetic(nextChar)) {
  27. // that is another, easier way to to the same thing
  28. // it`s ok to be in a nested if-statement because they do the same thins
  29. // if one is true the other will be true as well
  30.  
  31. if (nextChar == 'c' && !c) { // in nextChar is c, we check if we have encountered it yet
  32. c = true; // if not, we change the boolean that tracks that to true and do nothing further
  33. } else if (nextChar == 'o' && !o) {
  34. o = true; // the same goes for the other two chars
  35. } else if (nextChar == 'n' && !n) {
  36. n = true;
  37. } else { // in all other cases we append the nextChar to out word
  38. nextWord += nextChar;
  39. }
  40. }
  41. }
  42. if (c && o && n) { // if we have encountered all three chars, we print out word and reset out tracker variables as well as the word itself
  43. System.out.print(nextWord + " ");
  44. c = false;
  45. o = false;
  46. n = false;
  47. nextWord = "";
  48. }
  49.  
  50. nextLine = scanner.nextLine();
  51. // do not forget to read a new line at the end of the while loop
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement