Untitled
unknown
plain_text
a month ago
3.5 kB
1
Indexable
Never
import java.io.*; import java.lang.*; public class StringCounter { private static String readFile(String file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append(ls); } return stringBuilder.toString(); } finally { reader.close(); } } public static int count(String filename, String target) throws IOException { File file = new File(filename); BufferedReader br = new BufferedReader(new FileReader(file)); String st; int count = 0; try { while ((st = br.readLine()) != null) { int x = st.indexOf(target); while (x > -1) { count++; x = st.indexOf(target, x + target.length()); } } } catch (IOException excpt) { excpt.printStackTrace(); } return count; } public static int count(String filename, String target, String methodName) throws IOException { String st = readFile(filename); int count = 0; // Debug: Print the full content read System.out.println("Content read from file: \n" + st); // Find the index where methodName appears int methodIndex = st.indexOf(methodName + "("); System.out.println("Method Index: " + methodIndex); // Debug if (methodIndex == -1) { return 0; } // Find the opening brace for the method int openBraceIndex = st.indexOf("{", methodIndex); System.out.println("Open Brace Index: " + openBraceIndex); // Debug if (openBraceIndex == -1) { return 0; } // Initialize a variable to keep track of the number of open braces int openBraces = 1; // Start search after the first open brace int searchIndex = openBraceIndex + 1; while (openBraces > 0 && searchIndex < st.length()) { char c = st.charAt(searchIndex); if (c == '{') { openBraces++; } else if (c == '}') { openBraces--; } if (openBraces == 0) { break; } int targetIndex = st.indexOf(target, searchIndex); System.out.println("Target Index: " + targetIndex); // Debug if (targetIndex != -1 && targetIndex < st.indexOf("}", searchIndex)) { count++; searchIndex = targetIndex + target.length(); } else { searchIndex++; } } System.out.println("Count: " + count); // Debug return count; } public static void main(String[] args) { String filename = args[0]; String target = args[1]; int minCount = Integer.parseInt(args[2]); int maxCount = Integer.parseInt(args[3]); System.out.println("Checking:" + filename); try { int actualCount = 0; if (args.length > 4) { String methodName = args[4]; actualCount = count(filename, target, methodName); } else { actualCount = count(filename, target); } System.out.println("Found:" + actualCount + " \"" + target + "\"s in file " + filename); if (actualCount < minCount || actualCount > maxCount) { System.out.println("FAIL"); System.exit(1); } System.out.println("PASSED"); System.exit(0); } catch (IOException excpt) { System.out.println("FAILED"); System.exit(1); } } }