2014-12-19 112 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]+"."); 
} 

當打印結果。