-1

I am trying to figure out how to combine some of the lists so when you put in "BAD" or "Bad" it takes the B, A, and D and prints them out in one line of text.

import java.util.*;

public class Map4_for {

    public static void main(String[] args) {
        boolean repeat = (false);
        while(!repeat){
        Scanner in = new Scanner(System.in);
        String name = in.next();
        List <String> listClone = new ArrayList<String>();
            if(name.startsWith("A")|| name.equalsIgnoreCase("A")){
                listClone.add("Alpha");
                System.out.println(listClone);
            }
            else if(name.startsWith("B")|| name.equalsIgnoreCase("B")){
                listClone.add("Bravo");
                System.out.println(listClone);
            }
            else if(name.startsWith("C")|| name.equalsIgnoreCase("C")){
                listClone.add("Charlie");
                System.out.println(listClone);
            }
            else if(name.startsWith("D")|| name.equalsIgnoreCase("D")){
                listClone.add("Delta");
                System.out.println(listClone);
            }
            else if(name.startsWith("E")|| name.equalsIgnoreCase("E")){
                listClone.add("Echo");
                System.out.println(listClone);
            }
            else if(name.startsWith("F")|| name.equalsIgnoreCase("F")){
                listClone.add("Foxtrot");
                System.out.println(listClone);
            }
        }
    }
}
FailingCoder
  • 757
  • 1
  • 8
  • 20
Gizmo5012
  • 9
  • 1
  • 3
    How exactly do we combine lists? How many lists are there? 1? You say "...so when you put in "BAD" or "Bad" it takes the B, A, and D and prints them out in one line of text." In one line of text with no newline characters? Doesn't that just mean that it simply prints the input? Input: `BAD` Output: `BAD` Isn't this pointless then? – FailingCoder Nov 12 '19 at 20:15
  • Agree with FailingCoder, instructions are unclear. What lists would you like to combine? And why is your `false` within parentheses? – Doompickaxe Nov 12 '19 at 20:25
  • Do you mean that you enter a string, then the string is broken down into it's constituent letters and then these letters are mapped to the pop-culture military words? – Shankha057 Nov 12 '19 at 20:56

2 Answers2

0

If I understood you correctly you want to read a word from standard input, put it in a list, then split the word into individual letters, and then print them on the screen with commas between the letters? This could be done with a short loop iterating over each letter in the word and printing them with a comma after it unless it's the last letter.

I am also guessing that you want to keep one list for the duration of the program and did not intend to create a new list each time. Then you would want to move the list initialization to outside the while loop. Also, I see no reason for creating a new Scanner object each loop iteration so maybe you want to move that outside your while loop as well. Hope this helps.

ellpei
  • 81
  • 1
  • 4
0

First and foremost, move the Scanner initialization out of the loop. Secondly, create a HashMap all the alphabets and their corresponding pop-culture military sounding word (like map.put("A", "Alpha") and map.put("a", "Alpha")) like this:

HashMap<String, String> militaryJargonMap = new HashMap<>() {{
            put("A", "Alpha");put("B", "Beta");put("C", "Charlie");put("D", "Delta");
            put("a", "Alpha");put("b", "Beta");put("c", "Charlie");put("d", "Delta");
        }};  

Now split your input word into it's consecutive letters using the regex (?!^).
Then iterate through each element in the array obtained after splitting and then check if the letter is a key in the HashMap. If it is present then put the corresponding value into the List.

Using Streams it becomes like this:

Stream.of(str.split("(?!^)"))
                .map(s -> militaryJargonMap.getOrDefault(s, s))
                .collect(Collectors.toList())
                .forEach(System.out::println);
Shankha057
  • 1,296
  • 1
  • 20
  • 38
  • I'd use `Map militaryJargonMap = Map.of( "A", "Alpha", "B", "Bravo", "C", "Charlie"...);` instead of the double-brace syntax which some consider as [anti-pattern](https://www.baeldung.com/java-double-brace-initialization). – MartinBG Nov 12 '19 at 22:18
  • @MartinBG `Map.of()` doesn't tale in more than 10 key-value pairs. In the OP's case, ther needs to be actually 46 key-value pairs (for both upper and lower case letters). So the method `Map.of` won't work. The alternative is to use the `Map.ofEntries` method. – Shankha057 Nov 12 '19 at 22:36
  • 1
    @MartinBG Also, these are the kind of fields that are made `static` and hence the issue of a memory leak isn't there. And I'm really this just need to get initialized just once and then you keep using it. So the class loading overhead is also negligible. Also, **this is just for solving the main problem and I never did or do or will use this in production code**. – Shankha057 Nov 16 '19 at 20:35