Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Is this syntax possible?
for (int counter=o,charArray[0]; counter <=10,charArray[] <=0; counter++, charArray[]++)


I am asking this since I am having a hard time with the code I'm making. I'mplaying around with a string searching code and I want to use that for the following text file with this content:
KA-MA-NA-PA-QA
AA-DA-IA-MA
AA-BA-DA-EA-FA-HA
DA-EA-IA-MA-NA
EA-GA-HA-JA-PA-QA
BA-GA-IA-MA-LA
BA-EA-IA-KA-LA-PA
AA-FA-JA-KA-LA
CA-GA-HA-LA-MA
CA-EA-FA-GA-LA-PA
BA-CA-JA-MA
BA-DA-GA-HA-IA-KA
AA-FA-MA-OA-PA
CA-FA-HA-IA-JA-PA
AA-JA-LA-NA-QA
AA-BA-CA-IA-NA-QA
IA-JA-MA-PA
CA-DA-FA-GA-HA-QA
HA-IA-KA-LA-NA
FA-MA-OA-PA
EA-GA-LA-OA-QA
BA-EA-GA-IA-QA
AA-HA-IA-MA-PA
CA-DA-KA-OA-PA

What I intended for the source code I made is that it should search through only 12 lines in the file, instead of all 24 lines, for any repeating occurrence of a character. From what I did, the code goes through the entire list 12 times instead of going through 12 lines for one loop only. I believed that only a "for" loop can achieve the action I want - though I don't know where in the code I should place it. By the way, I included the code below:

import java.io.*;
import java.util.*;

public class stringsearch{
	public static void main (String args [])throws IOException {
		Scanner search = new Scanner (new File ("stringtext.txt"));
		
		int counterA=0;
		int counterB=0;
		int counterC=0;
		int counterD=0;
		int counterE=0;
		int counterF=0;
		int counterG=0;
		int counterH=0;
		int counterI=0;
		int counterJ=0;
		int counterK=0;
		int counterL=0;
		int counterM=0;
		int counterN=0;
		int counterO=0;
		int counterP=0;
		int counterQ=0;	
			
		int arrayindex=0;
		
		while (search.hasNextLine())
		     { //while loop starting brace
			  String scanline = search.nextLine();
			  String [] charArray = scanline.split("-");
			  for (int counter=0; counter<=42; counter++)
			     { // for loop starting brace
			      if (charArray[arrayindex].equals("AA"))
			        {  //first main outer if loop starting brace
				     counterA++;
						if (counterA>=3) {
				           				  System.out.println ("AA is very common");
				                  		 } else {
	    				 				 	     System.out.println ("AA is least common");
				                  				}
			        }  //first main outer if loop ending brace
			 else if (charArray[arrayindex].equals("BA"))
			        { //second main outer if loop starting brace
				     counterB++;
						if (cluster_B_counter>=3) {
				           						   System.out.println ("BA is very common");
				                  				  } else {
	    				 								  System.out.println ("BA is very common");
				                  				         }		         
			        } //second main outer if loop ending brace
			     } // for loop ending brace
		     }//while loop ending brace
	}
}


Do you have any suggestions on how I should make the code work the way I want it?
Posted
Updated 22-Apr-14 20:40pm
v2

1 solution

This is a classic homework task - so I added the tag.
But you provided code -> +5 for that!

You are using a while loop - that one runs until the list is done.
So just switch that one to a for loop to get the desired 12 lines running.
Or add a counter and count the number of lines you've run through.

there is another for loop in there:

Java
for (int counter=0; counter<=42; counter++){
  // fancy code
}


Why is that one counting to 42?
 
Share this answer
 
Comments
code_flea 23-Apr-14 7:44am    
As I said, I was playing around with the code - so I ran that to 42. Uhm - wait, you want me to change the "while" loop into a "for" loop? :-/ Wouldn't that cause the program to run through the entire list as determined by the counter and not through only 12 lines? :-/
TorstenH. 23-Apr-14 7:55am    
You said you wanted to run only 12 line and not the entire list.
The wihle loop goes through the complete list, and for every value the for loop is made 42 times.

Put some logging in (just some regular System.out.println("TEXT") will do) to get a better understanding what is happening when.
code_flea 23-Apr-14 8:06am    
Okay - will try it and see.... Thanks by the way :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900