CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2013
    Posts
    8

    String counter doesn't count the appropriate strings properly

    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...
    Name:  output for version01 source.jpg
Views: 578
Size:  55.5 KB
    Name:  output for version02 source.jpg
Views: 503
Size:  55.4 KB

    I couldn't place the break statement anywhere in the code without causing an error. Is there something I missed out? Here are two versions of the code by the way:
    Version 01 -
    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 -
    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();				        		
    	}
    }

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: String counter doesn't count the appropriate strings properly

    I think you should rewrite your code.

    Read each line from file separeted by "-". Then have a HashMap, check if you have the key there, if you have increase the value by one, if not add it and set value to 1.

  3. #3
    Join Date
    Apr 2013
    Posts
    8

    Re: String counter doesn't count the appropriate strings properly

    I pulled off the source code from a notepad copy. The counter was supposed to be a "5" and not "10".
    Unfortunately, though the counter value is set at 5, it still doesn't work.
    What's wrong with using "Scanner"?

  4. #4
    Join Date
    Apr 2010
    Posts
    13

    Re: String counter doesn't count the appropriate strings properly

    dont know if I missed something but why you have a break there?
    try to analyze your output... doesn't it look like it is only counting the first occurences from every line? think about it with the break position in mind..

    by the way, you shoul listen to laitinen for enhancing you algorithm, plus you should always run the for loop as many times as are the items in the (no-name) array it is processing (i.e. do not use the 10 or 5 const, try to get the items count from the array)
    Last edited by Zavael; May 16th, 2014 at 06:31 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured