-1

The user is supposed to enter multiple words (without regards to lower/uppercase) with space inbetween, this text will then be translated into initials without any spaces included. However, I only want the initials of the words that I approve of, if anything but those words, the printout will instead say "?" instead of printing the first alphabet of a word. E.q: "hello hello hello" will come out as: "HHH", but "hello hi hello" or "hello . hello" will instead result in "H?H".

I've managed to make it print out the initials without the spaces. But I can't figure out how to add a condition where the program would first check whether the input contains unapproved words or signs/symbol or not in order to replace that unapproved or non-word with a question mark instead of just going ahead and printing the initial/sign. I've tried placing the for-loop inside an if-else-loop and using a switch()-loop but they won't interact with the for-loop correctly.

public static void main (String []args) {

    Scanner keyboard = new Scanner (System.in);
    System.out.println("Enter your words: ");

    String input = keyboard.nextLine().toUpperCase();
    String str = input;
    String[] parts = str.split(" ");

    System.out.print("The initials: ");
    for (String i : parts) {
        System.out.print(i.charAt(0));
    }
}

So what happens right now is that regardless what words the user enter, the initials of each word or symbol/sign will be printed regardless.

Bucc Olam
  • 5
  • 5
  • Inside the for loop you can add "If ( condition ) " { System.out.print(i.charAt(0)); } else { do nothing }, I'd suggest the condition is checking if your "parts" exists in a list of approved words. – GamingFelix May 08 '19 at 10:59

4 Answers4

0

You should create a set of approved words and then check whether each word, entered by the user, makes part of this set.

Something like this:

...
Set<String> approved_words = new TreeSet<>();
approved_words.add("HELLO");
approved_words.add("GOODBYE");
...
for (String i : parts) {
  if (approved_words.contains(i))
    System.out.print(i.charAt(0));
  else
    System.out.print('?');
}
System.out.println();

Small suggestion:
You may want to allow the user to enter multiple spaces between the words.
In that case, split the words like this: str.split(" +")

Robert Kock
  • 5,795
  • 1
  • 12
  • 20
0

If you want to filter against words you dislike, you will have to code it.
Like the example with "hi":

public static void main (String []args) {

    Scanner keyboard = new Scanner (System.in);
    System.out.println("Enter your words: ");

    String input = keyboard.nextLine().toUpperCase();
    String str = input;
    String[] parts = str.split(" ");

    System.out.print("The initials: ");
    for (String i : parts) {
        if(!"HI".equals(i))
          System.out.print(i.charAt(0));
        else
          System.out.print("?");
    }
}

Of course in real life you want such comparison on a collection, preferably something fast, like a HashSet:

public static void main (String []args) {

    Set<String> bannedWords=new HashSet<String>(Arrays.asList("HI","."));

    Scanner keyboard = new Scanner (System.in);
    System.out.println("Enter your words: ");

    String input = keyboard.nextLine().toUpperCase();
    String str = input;
    String[] parts = str.split(" ");

    System.out.print("The initials: ");
    for (String i : parts) {
        if(!bannerWords.contains(i))
          System.out.print(i.charAt(0));
        else
          System.out.print("?");
    }
}

(this one bans 'hi' and '.')

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

You could create a simple Set containing all the words that you accept. Then in your for loop, for every String i you check if the set contains ``i```. If this is true, you print out i.charAt(0). Otherwise you print out "?". I could provide code for this if necessary, but it's always good to figure it out yourself ;)

0

Supposed you provide unapproved words as input arguments

public static void main (String []args) {
    Set<String> unapprovedSet = new HashSet<>(Arrays.asList(args));
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter your words: ");

    String input = keyboard.nextLine().toUpperCase();
    String[] parts = input.split(" ");

    System.out.print("The initials: ");
    for (String i : parts) {
        if (unapprovedSet.contains(i)) {
            System.out.print("?");
        } else {
            System.out.print(i.charAt(0));
        }
    }
}
HieuHT
  • 459
  • 5
  • 13