Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.softuni.whileloop.exercise;
- import java.util.Scanner;
- public class StreamOfCharacters {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // these will keep track of whether we have encountered the "magic" chars
- boolean c = false;
- boolean o = false;
- boolean n = false;
- String nextWord = "";
- String nextLine = scanner.nextLine();
- while (!"End".equals(nextLine)) {
- // we will be reading lines until the "End" command is entered
- char nextChar = nextLine.charAt(0);
- // the input is converted to char by taking the first letter of the input string
- // in this case the input only has one letter, so we take it
- if ((nextChar >= 'A' && nextChar <= 'Z') || (nextChar >= 'a' && nextChar <= 'z')) {
- // this is how we check if nextChar is a latin letter
- if (Character.isAlphabetic(nextChar)) {
- // that is another, easier way to to the same thing
- // it`s ok to be in a nested if-statement because they do the same thins
- // if one is true the other will be true as well
- if (nextChar == 'c' && !c) { // in nextChar is c, we check if we have encountered it yet
- c = true; // if not, we change the boolean that tracks that to true and do nothing further
- } else if (nextChar == 'o' && !o) {
- o = true; // the same goes for the other two chars
- } else if (nextChar == 'n' && !n) {
- n = true;
- } else { // in all other cases we append the nextChar to out word
- nextWord += nextChar;
- }
- }
- }
- 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
- System.out.print(nextWord + " ");
- c = false;
- o = false;
- n = false;
- nextWord = "";
- }
- nextLine = scanner.nextLine();
- // do not forget to read a new line at the end of the while loop
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement