简体   繁体   中英

Java storing the inputs from scanner

When I can't use the 'list', but need to store the inputs from the scanner, how can I deal with it?

I want to make the program counting the frequencies of the inputs appearing in the inputs from scanner.

For example, if the input is

"I like apple but I like banana too"

The result is

I: 2 like: 2 apple: 1 banana: 1 but: 1 too: 1

At first I thought of making the arrays of string, and every time input comes, I put them in the array. After then, although it would require n^2 time complexity, run the for loop for each element and then check if it has the same word.

    for (String str in arr){
        for(String str_2 in arr){
             if(strr.equals(str_2)){
                    count[i]++;
             } // count is the array storing the frequencies.

But problem here is... when declaring the arr, I should know the input size. Other people told me to use "list" , but the usage of the "list" is restricted.

In this situation, what would be the way?

Can you use Java streams ?

String[] array = {"i", "like", "apple", "but", "i", "like", "banana", "too"};

Or to get the input from the user, something like:

Scanner sc = new Scanner(System.in);
int numberOfEntries = sc.nextInt(); // defines how big the array should be
String[] array = new String[numberOfEntries];
for (int i = 0; i < numberOfEntries; i++) {
    System.out.println("Enter value " + (i+1));
    String word = sc.next();
    array[i] = word;
}

Arrays.stream(array).collect(Collectors.groupingBy(p -> p, Collectors.counting()))
                .entrySet().stream().forEach(key -> System.out.println(key.getKey() + ": " + key.getValue()));

Output:

banana: 1

but: 1

apple: 1

too: 1

like: 2

i: 2

You can do something like this by using a HashMap and iterating over the input array in a for loop and checking if the map already contains the key or not. If it contains then just increase the count in the value. If the key is not already in the map, just add it alongwith value 1.

for (String str : inputArray) {
    if (map.containsKey(str)) {
        map.put(str, map.get(str) + 1);
    } else {
        map.put(str, 1);
    }
}

Finally, just iterate over the map and print with key and value pairs.

  String input = "I like apple but I like banana too";
    String[] words = input.split(" ");
    int countfre=0;
    HashMap<String,Integer> map = new HashMap<String, Integer>();
    for(int i=0;i<words.length;i++){
        if(!map.containsKey(words[i])){
            for (int j=0;j<words.length;j++){
                if(words[i].equalsIgnoreCase(words[j])){
                    countfre++;
                }
                map.put(words[i],countfre);

            }
            countfre=0;
            System.out.println(words[i] + " = " +map.get(words[i]));

        }

    }

//Without java stream api. //Stored output in Map.

Output:

banana = 1

but = 1

apple = 1

too = 1

like = 2

i = 2

Try something like,

public static void main(String[] args) {
        //input
        String s = "I like apple but I like banana too";
        //desired output
        //I: 2 like: 2 apple: 1 banana: 1 but: 1 too: 1

        String[] str = s.split(" ");
        String[] result = new String[str.length];
        int temp = 0;

        test:
        for (String str1 : str) {

            for (String str2 : result) {
                if(str1.equals(str2)){
                    continue test;
                }
            }
            result[temp++] = str1;
            int count = 0;
            for (String str2 : str) {
                 if(str1.equals(str2)){
                     count++;
                 }
            }
            System.out.print(str1 + ": " + count + " ");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM