计算出现的字长

时间:2014-12-19 11:17:17

标签: java arrays string

我正在做一个程序来计算每个单词的长度,然后计算该长度的出现次数。

例如:

Enter a String :I love my work
The word count is -
No. of words of length 1 are 1.
No. of words of length 2 are 1.
No. of words of length 4 are 2. 

到目前为止,我试过这个,

import java.util.Scanner;

class Demo{
    public static void main(String[] args){
        String s;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String :");
        s=sc.nextLine();
        String[] arr = s.split(" ");
        String str = "";
        int [] len = new int[arr.length];
        int [] count = new int[arr.length];
        int c = 0;
        for(int i=0;i<arr.length;i++){
            str = arr[i];
            len[i] = str.length();

            for(int j=0;j<arr.length;j++){
                if(str.length() == arr[j].length()){
                    count[i] = ++c;
                }
            }
            c = 0;
        }

        for(int i=0;i<len.length;i++){

            System.out.println("No. of words of length "+len[i]+" are "+count[i]+".");

        }

    }
}

我的逻辑存在问题,这就是为什么它的输出是这样的:

Enter a String :I love my work
    The word count is -
    No. of words of length 1 are 1.
    No. of words of length 2 are 1.
    No. of words of length 4 are 2.
    No. of words of length 4 are 2.

任何建议如何解决这个或任何其他更简单的方法(不使用集合,地图)。

4 个答案:

答案 0 :(得分:2)

您可以将array替换为Map<Integer,Integer>,这将很容易。

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a String :");
    String s = sc.nextLine();
    String[] arr = s.split(" ");// get the words
    Map<Integer, Integer> lengthVsCount=new HashMap<>(); // length vs count
    for(String i:arr){ // iterate array
        Integer val=lengthVsCount.get(i.length()); // searching count
        if(val!=null){ // if count is there
            lengthVsCount.put(i.length(),val+1);// increment count by one
        }else{ // count not there
            lengthVsCount.put(i.length(),1); // add count as one
        }
    }
    for (Map.Entry<Integer,Integer> entry:lengthVsCount.entrySet()) {
        System.out.println("No. of words of length " + entry.getKey() + " are " + entry.getValue() + ".");
    }

答案 1 :(得分:1)

你应该使用地图:

public static void main(String[] args) {
    final String input = "I love my work";
    final String[] words = input.split(" ");
    final Map<Integer, Integer> occurencesMap = new HashMap<Integer, Integer>();
    for (final String word : words) {
        final int lenght = word.length();
        if (occurencesMap.get(lenght) == null) {
            occurencesMap.put(lenght, 1);
        } else {
            occurencesMap.put(lenght, occurencesMap.get(lenght) + 1);
        }
    }
    System.out.println("The word count is -");
    final Iterator<Map.Entry<Integer, Integer>> entries = occurencesMap.entrySet().iterator();
    while (entries.hasNext()) {
        final Map.Entry<Integer, Integer> entry = entries.next();
        System.out.println("No. of words of length " + entry.getKey() + " are " + entry.getValue());
    }
}

答案 2 :(得分:0)

class Demo{
  public static void main(String[] args){
    String s;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter a String :");
    s=sc.nextLine();
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

    for (String str : s.split(" ")) {
        int key = str.length();
        if (map.containsKey(key)) {
            map.put(key, map.get(key)+1);
        }
        else {
            map.put(key, 1);
        }
    }

    Iterator<Integer> iterator =  map.keySet().iterator();
    while(iterator.hasNext()) {
        int key = iterator.next();
        System.out.println("No. of words of length " + key + " are " + map.get(key) + ".");
    }

  }
}

答案 3 :(得分:0)

下面,

for(int i=0;i<arr.length;i++){
    str = arr[i];
    len[i] = str.length();

您应该检查len[i]是否不是数组中已有的值。所以使用

int k;
for(k=0 ; k<i ; k++ )
    if( len[i]==len[k] ) //if a match was found
        break; //break out of the loop
if(k!=i) //will be true if the break has been executed
{
    len[i]=0; //set to 0
    continue; //go back to the loop
}

在此之后使用

for(int i=0;i<len.length;i++){
    if(len[i]!=0)
    System.out.println("No. of words of length "+len[i]+" are "+count[i]+".");
}

打印结果时。

相关问题