Java存储来自扫描仪的输入

时间:2018-10-23 12:08:21

标签: java arrays

当我无法使用“列表”,但需要存储扫描仪的输入时,我该如何处理呢?

我想让程序计算出现在扫描仪输入中的输入频率。

例如,如果输入为

“我喜欢苹果,但我也喜欢香蕉”

结果是

我:2 喜欢:2 苹果:1 香蕉:1 但是:1 也是:1

起初,我想到了制作字符串数组的方法,每次输入时,我都会将它们放入数组中。之后,尽管这将需要n ^ 2的时间复杂度,但是请为每个元素运行for循环,然后检查其是否具有相同的单词。

    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.

但是这里的问题是...在声明arr时,我应该知道输入大小。其他人告诉我使用“ list”,但是“ list”的使用受到限制。

在这种情况下,怎么办?

4 个答案:

答案 0 :(得分:3)

您可以使用Java streams吗?

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

或者从用户那里获取输入,例如:

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()));

输出:

  

香蕉:1

     

但是:1

     

苹果:1

     

太:1

     

like:2

     

i:2

答案 1 :(得分:0)

您可以通过使用HashMap并在for循环中遍历输入数组并检查映射是否已包含键来执行类似的操作。如果包含,则只需增加值的计数即可。如果该键尚未在地图中,则只需将其与值1一起添加即可。

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

最后,只需遍历地图并使用键和值对进行打印。

答案 2 :(得分:0)

  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]));

        }

    }

//没有Java流api。 //将输出存储在Map中。

  

输出:

     

香蕉= 1

     

但是= 1

     

苹果= 1

     

太= 1

     

like = 2

     

i = 2

答案 3 :(得分:0)

尝试类似的东西

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 + " ");
        }
相关问题