繁体   English   中英

Java存储扫描仪的输入

[英]Java storing the inputs from scanner

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

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

例如,如果输入是

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

结果是

我: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时,我应该知道输入大小。 其他人告诉我使用“列表”,但是“列表”的使用受到限制。

在这种情况下,应该怎么办?

可以使用Java 吗?

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

喜欢:2

我:2

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

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

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

  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

喜欢= 2

我= 2

尝试类似的东西,

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 + " ");
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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