I have this source code that is supposed to count the repeated occurrences of certain strings in the text file, "string.txt".
The contents of the file are listed below:
But as I checked it against the actual contents, the output program isn't counting the occurrences properly. Instead of going through 10 lines as needed, it seems that it is skipping some lines.AA-AB-AC-AG-AE-AL
AL-AQ-AE-AN-AO-AM
AM-AQ-AO-AA-AB-AC
AD-AJ-AK-AI-AM-AO
AN-AH-AC-AA-AP-AQ
AP-AP-AN-AN-AL-AP
AN-AD-AU-AE-AH-AQ
AK-AQ-AE-AL-AE-AA
AA-AJ-AB-AG-AE-AF
AF-AE-AL-AI-AP-AM
AU-AF-AJ-AV-AI-AM
AP-AI-AQ-AK-AB-AC
AU-AN-AL-AF-AO-AB
AM-AA-AN-AM-AH-AO
AP-AQ-AJ-AD-AN-AI
AG-AU-AO-AD-AF-AI
AQ-AM-AN-AB-AC-AK
AN-AB-AI-AL-AE-AJ
AB-AA-AP-AQ-AH-AD
AC-AL-AG-AU-AJ-AM
AL-AP-AB-AM-AA-AC
AD-AE-AA-AC-AL-AB
AO-AN-AQ-AK-AM-AQ
AE-AI-AC-AH-AD-AJ
AJ-AN-AM-AK-AL-AP
I have made two versions of the code where the placement of the break statement varies. But in both versions, the same problem persists
(that is, the occurrences aren't counted properly). "AH" and "AI" are supposed to be counted since they are within the required first 10 lines - but for some reason, they aren't included...
output for version01 source.jpg
output for version02 source.jpg
I couldn't place the break statement anywhere in the code without causing an error. Is there something I missed out?
Below are two versions of the code by the way:
Version 01 source code>
import java.io.*; import java.util.*; public class stringcounter_ver01{ public static void main (String args [])throws IOException { Scanner search = new Scanner (new File ("string.txt")); Scanner record = new Scanner (new File ("output.txt")); int counterAA=0; int counterAB=0; int counterAC=0; int counterAD=0; int counterAE=0; int counterAF=0; int counterAG=0; int counterAH=0; int counterAI=0; int counterAJ=0; int counterAK=0; int counterAL=0; int counterAM=0; int counterAN=0; int counterAO=0; int counterAP=0; int counterAQ=0; String counterrecordAA=" "; String counterrecordAB=" "; String counterrecordAC=" "; String counterrecordAD=" "; String counterrecordAE=" "; String counterrecordAF=" "; String counterrecordAG=" "; String counterrecordAH=" "; String counterrecordAI=" "; String counterrecordAJ=" "; String counterrecordAK=" "; String counterrecordAL=" "; String counterrecordAM=" "; String counterrecordAN=" "; String counterrecordAO=" "; String counterrecordAP=" "; String counterrecordAQ=" "; String spacer="--------------------------------------------------------------"; int maxLines=10; while (search.hasNextLine() && maxLines>=0) { //while loop starting brace String scanline = search.nextLine(); String charArray[] = scanline.split("-"); for (int counter=0; counter<=10; counter++) { // for loop starting brace if (charArray[counter].equals("AA")) { //first main outer if loop starting brace counterAA++; counterrecordAA="AA has occurred " + counterAA + " times \t\n"; break; } //first main outer if loop ending brace else if (charArray[counter].equals("AB")) { //second main outer if loop starting brace counterAB++; counterrecordAB="AB has occurred " + counterAB + " times \t\n"; break; } //second main outer if loop ending brace else if (charArray[counter].equals("AC")) { //third main outer if loop starting brace counterAC++; counterrecordAC="AC has occurred " + counterAC + " times \t\n"; break; } //third main outer if loop ending brace else if (charArray[counter].equals("AD")) { //fourth main outer if loop starting brace counterAD++; counterrecordAD="AD has occurred " + counterAD + " times \t\n"; break; } //fourth main outer if loop ending brace else if (charArray[counter].equals("AE")) { //fifth main outer if loop starting brace counterAE++; counterrecordAE="AE has occurred " + counterAE + " times \t\n"; break; } //fifth main outer if loop ending brace else if (charArray[counter].equals("AF")) { //sixth main outer if loop starting brace counterAF++; counterrecordAF="AF has occurred " + counterAF + " times \t\n"; break; } //sixth main outer if loop ending brace else if (charArray[counter].equals("AG")) { //seventh main outer if loop starting brace counterAG++; counterrecordAG="AG has occurred " + counterAG + " times \t\n"; break; } //seventh main outer if loop ending brace else if (charArray[counter].equals("AH")) { //eighth main outer if loop starting brace counterAH++; counterrecordAH="AH has occurred " + counterAH + " times \t\n"; break; } //eighth main outer if loop ending brace else if (charArray[counter].equals("AI")) { //ninth main outer if loop starting brace counterAI++; counterrecordAI="AI has occurred " + counterAI + " times \t\n"; break; } //ninth main outer if loop ending brace else if (charArray[counter].equals("AJ")) { //10th main outer if loop starting brace counterAJ++; counterrecordAJ="AJ has occurred " + counterAJ + " times \t\n"; break; } //10th main outer if loop ending brace else if (charArray[counter].equals("AK")) { //11th main outer if loop starting brace counterAK++; counterrecordAK="AK has occurred " + counterAK + " times \t\n"; break; } //11th main outer if loop ending brace else if (charArray[counter].equals("AL")) { //12th main outer if loop starting brace counterAL++; counterrecordAL="AL has occurred " + counterAL + " times \t\n"; break; } //12th main outer if loop ending brace else if (charArray[counter].equals("AM")) { //13th main outer if loop starting brace counterAM++; counterrecordAM="AM has occurred " + counterAM + " times \t\n"; break; } //13th main outer if loop ending brace else if (charArray[counter].equals("AN")) { //14th main outer if loop starting brace counterAN++; counterrecordAN="AN has occurred " + counterAN + " times \t\n"; break; } //14th main outer if loop ending brace else if (charArray[counter].equals("AO")) { //15th main outer if loop starting brace counterAO++; counterrecordAO="AO has occurred " + counterAO + " times \t\n"; break; } //15th main outer if loop ending brace else if (charArray[counter].equals("AP")) { //16th main outer if loop starting brace counterAP++; counterrecordAP="AP has occurred " + counterAP + " times \t\n"; break; } //16th main outer if loop ending brace else if (charArray[counter].equals("AQ")) { //17th main outer if loop starting brace counterAQ++; counterrecordAQ="AQ has occurred " + counterAQ + " times \t\n"; break; } //17th main outer if loop ending brace } // for loop ending brace }//while loop ending brace FileWriter recorder=new FileWriter("output.txt",true); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAA+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAB+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAC+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAD+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAE+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAF+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAG+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAH+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAI+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAJ+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAK+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAL+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAM+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAN+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAO+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAP+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAQ+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.close(); } }
Version 02 source code>
import java.io.*; import java.util.*; public class stringcounter_ver02{ public static void main (String args [])throws IOException { Scanner search = new Scanner (new File ("string.txt")); Scanner record = new Scanner (new File ("output.txt")); int counterAA=0; int counterAB=0; int counterAC=0; int counterAD=0; int counterAE=0; int counterAF=0; int counterAG=0; int counterAH=0; int counterAI=0; int counterAJ=0; int counterAK=0; int counterAL=0; int counterAM=0; int counterAN=0; int counterAO=0; int counterAP=0; int counterAQ=0; String counterrecordAA=" "; String counterrecordAB=" "; String counterrecordAC=" "; String counterrecordAD=" "; String counterrecordAE=" "; String counterrecordAF=" "; String counterrecordAG=" "; String counterrecordAH=" "; String counterrecordAI=" "; String counterrecordAJ=" "; String counterrecordAK=" "; String counterrecordAL=" "; String counterrecordAM=" "; String counterrecordAN=" "; String counterrecordAO=" "; String counterrecordAP=" "; String counterrecordAQ=" "; String spacer="--------------------------------------------------------------"; int maxLines=10; while (search.hasNextLine() && maxLines>=0) { //while loop starting brace String scanline = search.nextLine(); String charArray[] = scanline.split("-"); for (int counter=0; counter<=10; counter++) { // for loop starting brace if (charArray[counter].equals("AA")) { //first main outer if loop starting brace counterAA++; counterrecordAA="AA has occurred " + counterAA + " times \t\n"; } //first main outer if loop ending brace else if (charArray[counter].equals("AB")) { //second main outer if loop starting brace counterAB++; counterrecordAB="AB has occurred " + counterAB + " times \t\n"; } //second main outer if loop ending brace else if (charArray[counter].equals("AC")) { //third main outer if loop starting brace counterAC++; counterrecordAC="AC has occurred " + counterAC + " times \t\n"; } //third main outer if loop ending brace else if (charArray[counter].equals("AD")) { //fourth main outer if loop starting brace counterAD++; counterrecordAD="AD has occurred " + counterAD + " times \t\n"; } //fourth main outer if loop ending brace else if (charArray[counter].equals("AE")) { //fifth main outer if loop starting brace counterAE++; counterrecordAE="AE has occurred " + counterAE + " times \t\n"; } //fifth main outer if loop ending brace else if (charArray[counter].equals("AF")) { //sixth main outer if loop starting brace counterAF++; counterrecordAF="AF has occurred " + counterAF + " times \t\n"; } //sixth main outer if loop ending brace else if (charArray[counter].equals("AG")) { //seventh main outer if loop starting brace counterAG++; counterrecordAG="AG has occurred " + counterAG + " times \t\n"; } //seventh main outer if loop ending brace else if (charArray[counter].equals("AH")) { //eighth main outer if loop starting brace counterAH++; counterrecordAH="AH has occurred " + counterAH + " times \t\n"; } //eighth main outer if loop ending brace else if (charArray[counter].equals("AI")) { //ninth main outer if loop starting brace counterAI++; counterrecordAI="AI has occurred " + counterAI + " times \t\n"; } //ninth main outer if loop ending brace else if (charArray[counter].equals("AJ")) { //10th main outer if loop starting brace counterAJ++; counterrecordAJ="AJ has occurred " + counterAJ + " times \t\n"; } //10th main outer if loop ending brace else if (charArray[counter].equals("AK")) { //11th main outer if loop starting brace counterAK++; counterrecordAK="AK has occurred " + counterAK + " times \t\n"; } //11th main outer if loop ending brace else if (charArray[counter].equals("AL")) { //12th main outer if loop starting brace counterAL++; counterrecordAL="AL has occurred " + counterAL + " times \t\n"; } //12th main outer if loop ending brace else if (charArray[counter].equals("AM")) { //13th main outer if loop starting brace counterAM++; counterrecordAM="AM has occurred " + counterAM + " times \t\n"; } //13th main outer if loop ending brace else if (charArray[counter].equals("AN")) { //14th main outer if loop starting brace counterAN++; counterrecordAN="AN has occurred " + counterAN + " times \t\n"; } //14th main outer if loop ending brace else if (charArray[counter].equals("AO")) { //15th main outer if loop starting brace counterAO++; counterrecordAO="AO has occurred " + counterAO + " times \t\n"; } //15th main outer if loop ending brace else if (charArray[counter].equals("AP")) { //16th main outer if loop starting brace counterAP++; counterrecordAP="AP has occurred " + counterAP + " times \t\n"; } //16th main outer if loop ending brace else if (charArray[counter].equals("AQ")) { //17th main outer if loop starting brace counterAQ++; counterrecordAQ="AQ has occurred " + counterAQ + " times \t\n"; } //17th main outer if loop ending brace break; } // for loop ending brace }//while loop ending brace FileWriter recorder=new FileWriter("output.txt",true); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAA+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAB+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAC+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAD+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAE+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAF+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAG+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAH+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAI+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAJ+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAK+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAL+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAM+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAN+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAO+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAP+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.write(counterrecordAQ+System.getProperty("line.separator")); recorder.write(spacer+System.getProperty("line.separator")); recorder.close(); } }