Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Is this syntax possible?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is this syntax possible?

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,300
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Is this syntax possible?

    What happened when you compiled it?

    It looks like you're just trying to access the indexes of charArray in your loop. Something like:

    for (int counter=o; counter <=10; counter++){
       char c = charArray[counter];

    If that's not what you want, can you be more specific about what you want the loop behavior to be?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Is this syntax possible?

    Quote Originally Posted by PC_flea View Post
    Is this syntax possible?
    for (int counter=o,charArray[0]; counter <=10,charArray[] <=0; counter++, charArray[]++)
    No, it is not possible. See The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) for the general form of the for statement.

    For the initialisation expression, you can initialise 2 or more variables but only of the same type. E.g., the following initialises 2 int variables, counter and counter2:
    for (int counter = 0, counter2 = 0; ...; ...)

    The termination expression must be an expression that can be evaluated to true or false. Therefore "counter <=10,charArray[] <=0" is invalid. However you can combine 2 or more expressions together using boolean logical operators such as && and || so that the expressions can as a whole be evaluated to true/false. E.g., the following is valid:
    for (...; counter <= 10 || intArray[counter2] <= 20; ...)

    Finally, your increment expression "counter++, charArray[]++" is also problematic. You can increment multiple variables in the expression, but charArray[]++ is simply invalid syntax. Instead, the following is valid:
    for (... ; ...; counter++, intArray[counter2]++)
    where "intArray[counter2]++" increments the value of the integer element stored in the intArray array, at the position provided by the counter2 variable.

    Having valid syntax is one thing. Using multiple variables and combining expressions such as in the examples above are error-prone, and make your code hard to read and understand. I wouldn't recommend it.

    Quote Originally Posted by PC_flea View Post
    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.
    I'm not sure if I completely understand the direction your code is heading. E.g., with
    for (int counter = 0; counter <= 42; counter++)
    why the magic number 42?

    If I were to implement this, I'd use a HashMap. The HashMap's keys would contain the words (KA, MA, AA, etc.) and its values would be the number of occurrence for the corresponding key (word).

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is this syntax possible?

    Quote Originally Posted by KevinWorkman View Post
    What happened when you compiled it?

    It looks like you're just trying to access the indexes of charArray in your loop. Something like:

    for (int counter=o; counter <=10; counter++){
       char c = charArray[counter];

    If that's not what you want, can you be more specific about what you want the loop behavior to be?
    As I have written in my question, I intend to search through only 12 lines of strings in the text. By the way, what you said made me wonder: as for all the lines in the text (when those get stored into the array), does the computer read them as one really long straight line of characters? Does it read them from the text file as is? (that is, each string on a line of its own)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Is this syntax possible?

    does the computer read them as one really long straight line of characters?
    Yes, it read bytes from the HDD. The higher level software then can break the bytes up into lines (ending with line end characters), create chars, build a String and return that from a readLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Syntax Question...
    By Praetorian in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 8th, 2014, 03:54 AM
  2. Syntax of main
    By RKM904 in forum The Cafe
    Replies: 2
    Last Post: September 7th, 2012, 01:09 PM
  3. Syntax in method
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: September 23rd, 2011, 03:01 PM
  4. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 03:01 PM
  5. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 03:09 PM

Tags for this Thread