2014-12-19 126 views
0

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

例如:

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. 

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

+3

首先你应该考虑使用HashMap来存放计数器。地图<“您计数的数字”,“发生次数”>是解决问题的标准方法。 – 2014-12-19 11:20:01

+1

您将为每个相同长度的字获得冗余输出。如Marcin指出的那样使用Map。 – 2014-12-19 11:22:05

回答

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

upadated question ...不使用地图或集合...坚持字符串,数组 – Leo 2014-12-19 11:27:07

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

upadated问题...不使用地图或集合...坚持字符串,数组 – Leo 2014-12-19 11:27:59

+0

@Leo,但为什么?在这种情况下使用地图更容易和更好 – MihaiC 2014-12-19 11:28:47

+0

是啊知道@MihaiC ......但这是挑战。 – Leo 2014-12-19 11:30:18

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

    } 
} 
+0

你已经注意到,这是第三个使用Maps和OP注意的答案,他不会/不能使用地图? – Tom 2014-12-19 12:10:11

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

当打印结果。