Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

String counter doesn't count the appropriate strings properly

array

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 PC_flea

PC_flea

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 11 May 2014 - 07:46 PM

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:

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
 

 

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.
 
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... (kindly see the attachments for details)
 

Here are two versions of the code by the way:
Version 01 -

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 -

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();          
}
}

Attached Thumbnails

  • output for version01 source.jpg
  • output for version02 source.jpg

Edited by PC_flea, 11 May 2014 - 07:51 PM.


#2 PC_flea

PC_flea

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 12 May 2014 - 06:59 AM

By the way, I made a typo in the above post...

 

The line....

 for (int counter=0; counter<=10; counter++)

...really has its counter value set at "5" and not "10". But in spite of that change, the same problem persists.... 

Any idea why that is happening? 



#3 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 12 May 2014 - 02:27 PM

Oh, my...  What a mess.  I won't even bother reading that.  :)  Never count occurrences of something like this.  You would be much better off using a Hashmap and incrementing the value for every key.  It's pretty easy.  Tons of examples on the google.   "Hashmap for counting occurence"


Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#4 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts

Posted 12 May 2014 - 03:55 PM

Also posted at: http://www.javaprogr...s-properly.html



#5 PBJ

PBJ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 176 posts

Posted 09 June 2014 - 05:24 PM

PC Flea,

You could also implement StringUtils.countMatches(targetString, subString) in conjunction with a HashMap as farrell2k suggested. These would be very efficient if implemented correctly.


Edited by PBJ, 09 June 2014 - 05:24 PM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download