2011-09-13 28 views
1

我已經寫了一些代碼來計算來自未知數量文件的「if」語句的數量。我怎樣才能保持每個文件的計數單獨和所有文件的「如果」共計?我如何跟蹤多個計數器變量

代碼:

import java.io.*; 

public class ifCounter4 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // variable to keep track of number of if's 
     int ifCount = 0; 

     for (int c = 0; c < args.length; c++) 
     { 
      // parameter the TA will pass in 
      String fileName = args[c]; 

      // create a new BufferReader 
      BufferedReader reader = new BufferedReader(new FileReader (fileName)); 
      String line = null; 
      StringBuilder stringBuilder = new StringBuilder(); 
      String ls = System.getProperty("line.separator"); 

      // read from the text file 
      while ((line = reader.readLine()) != null) 
      { 
       stringBuilder.append(line); 
       stringBuilder.append(ls); 
      } 

      // create a new string with stringBuilder data 
      String tempString = stringBuilder.toString(); 

      // create one last string to look for our valid if(s) in 
      // with ALL whitespace removed 
      String compareString = tempString.replaceAll("\\s",""); 

      // check for valid if(s) 
      for (int i = 0; i < compareString.length(); i++) 
      { 
       if (compareString.charAt(i) == ';' || compareString.charAt(i) == '}' || compareString.charAt(i) == '{') // added opening "{" for nested ifs :) 
       { 
        i++; 

        if (compareString.charAt(i) == 'i') 
        { 
         i++; 

         if (compareString.charAt(i) == 'f') 
         { 
          i++; 

          if (compareString.charAt(i) == '(') 
           ifCount++; 
         } // end if 
        } // end if 
       } // end if 

      } // end for 

     // print the number of valid "if(s) with a new line after" 
     System.out.println(ifCount + " " + args[c]); // <-- this keeps running total 
                 // but not count for each file 
     } 

     System.out.println(); 

    } // end main 
} // end class 
+1

你可以簡單地使用HashMap或一些這樣的,關鍵在與文件名稱映射,並存儲'if'在一個Integer對象計數地圖。總的來說,顯然,你會保持一個單獨的'int'值。 –

回答

-1

使用數組可以解決這個問題。

int[] ifCount = new int[args.length]; ,然後在循環ifCount[c]++;

+0

除了,如果arg [0]是一個選項而非文件,效果很好。我怎樣才能跳過它? – josh

+0

http://stackoverflow.com/a/14533881/1451716 –

+0

@ iah.vector什麼是downvote?數組中的每個int默認爲0,所以不需要實例化它們http://stackoverflow.com/a/2154340/839086 – Marcus

2

您可以創建一個存儲文件名作爲鍵和計數爲值的地圖。

Map<String, Integer> count = new HashMap<String, Integer>(); 

每個文件後,

count.put(filename, ifCount); 
ifcount = 0; 

步行路程獲得的總價值。

0

如何使用文件名作爲關鍵字並將ifs計數值保存爲值的地圖?對於整體計數,將其存儲在自己的int中,或者只需將Map中的所有值相加即可計算出來。

Map<String, Integer> ifsByFileName = new HashMap<String, Integer>(); 
int totalIfs = 0; 
for each if in "file" { 
    totalIfs++; 
    Integer currentCount = ifsByFileName.get(file); 
    if (currentCount == null) { 
     currentCount = 0; 
    } 
    ifsByFileName.put(file, currentCount + 1); 
} 

// total from the map: 
int totalIfsFromMap = 0; 
for (Integer fileCount : ifsByFileName.values()) { 
    totalIfsFromMap += fileCount; 
} 
-1

有問題的這種情況是許多線程需要增加一組相同的計數器的。

操作,如ifCount[c]++;ifsByFileName.put(file, currentCount + 1);線程安全

使用ConcurrentMapAtomicLong的明顯解決方案也不夠充分,因爲您必須將初始值設置爲0,這將需要額外的鎖定。

Google Guava項目提供了一個方便的開箱sollution的:AtomicLongMap

有了這個類,你可以這樣寫:

AtomicLongMap<String> cnts = AtomicLongMap.create(); 

cnts.incrementAndGet("foo"); 
cnts.incrementAndGet("bar"); 
cnts.incrementAndGet("foo"); 

for (Entry<String, Long> entry : cnts.asMap().entrySet()) { 
    System.out.println(entry); 
} 

它打印:

foo=2 
bar=1 

而且是完全線程安全。

-1

這是一個增加到100的計數器,如果你編輯N的值,它會在*的倍數旁邊加*。

公共類SmashtonCounter_multiples {

public static void main(String[] args) { 


    int count; 
    int n = 3; //change this variable for different multiples of 

    for(count = 1; count <= 100; count++) { 
     if((count % n) == 0) { 
       System.out.print(count + "*"); 
       } 
     else { 
       System.out.print(count); 

     if (count < 100) { 
     System.out.print(","); 
    } 
    } 
    } 

}