2011-09-13 29 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(","); 
    } 
    } 
    } 

}