Results 1 to 7 of 7
  1. #1
    JordiLaForge is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Exclamation Complicated for loop's

    Hello,

    I am trying to make an interpreter and so far it's working, I just think it can use a little more efficiency. The class is meant to take a string, compare it to a list of string and see if anywhere in the first string it matches anything in the list. Near line 41 is where it get's messy, see how every else statement it goes to another if statement with the same thing just with an added bit of code. Please help :)

    Java Code:
    public class checkText {
        
        static String[] inputMethod;
        
        static String stringConstruct;
        
        static int iteration;
        
        static boolean checkText;
    
    
        public static boolean main( String filePath , String[] userInput ){
    	
    	inputMethod = inputLoad.main ( filePath , "inputMethod" );
    	
    	checkText = false;
    	
    	iteration = 0;
    
    	for ( int x = 0 ; x <= userInput.length - 1 ; x = x + 1 ){
    	    
    		for ( int y = 0 ; y < inputMethod.length ; y = y + 1 ){
    		    
    		    for ( int z = 0 ; z <=  inputMethod[ y ].split( " " ).length ; z = z + 1 ){
    
    		    	if ( userInput[ x ].equals( inputMethod[ y ] ) ){
    			
    		    		checkText = true;
    			
    		    	}
    		    	else{
    			    
    		    		if ( ( x + 1 < userInput.length )  && (  z + 1 < inputMethod[ y ].split( " " ).length ) ){		    				
    			
    			    		if ( ( userInput[ x ].equals( inputMethod[ y ].split( " " )[ z ] ) )  && ( userInput[ x + 1 ].equals( inputMethod[ y ].split( " " )[ z + 1 ] ) ) ){
    				    
    			    			checkText = true;
    				    
    			    		}
    			    		else{
    			    			
    			    			if ( ( x + 2 < userInput.length )  && (  z + 2 < inputMethod[ y ].split( " " ).length ) ){
    			    				
    					    		if ( ( userInput[ x ].equals( inputMethod[ y ].split( " " )[ z ] ) )  && ( userInput[ x + 1 ].equals( inputMethod[ y ].split( " " )[ z + 1 ] ) ) && ( userInput[ x + 2 ].equals( inputMethod[ y ].split( " " )[ z + 2 ] ) ) ){
    								    
    					    			checkText = true;
    					    			
    					    		}
    					    		else{
    					    			
    					    			if ( ( x + 3 < userInput.length )  && (  z + 3 < inputMethod[ y ].split( " " ).length ) ){
    					    				
    							    		if ( ( userInput[ x ].equals( inputMethod[ y ].split( " " )[ z ] ) )  && ( userInput[ x + 1 ].equals( inputMethod[ y ].split( " " )[ z + 1 ] ) ) && ( userInput[ x + 2 ].equals( inputMethod[ y ].split( " " )[ z + 2 ] ) ) && ( userInput[ x + 3 ].equals( inputMethod[ y ].split( " " )[ z + 3 ] ) ) ){
    										    
    							    			checkText = true;
    							    			
    							    		}
    					    			
    					    			}
    					    			
    					    		}
    			    				
    			    			}
    			    			
    			    		}
    				
    		    		}
    			    
    		    	}
    			
    		    }
    		    
    		}
    		
    	}
    	
    	return checkText;
    	
        }
        
    }

  2. #2
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Re: Complicated for loop's

    Just a thought: I believe you're iterating over existing strings to search if a give string exists on that first collection. You could use maps or sets or something else based on hash tables to do that. Do you need a hint on this or is this sufficient?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Complicated for loop's

    To do this efficiently, you should learn about state machines, regular expressions, and lexical analyzers.
    Get in the habit of using standard Java naming conventions!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Complicated for loop's

    Yep! In fact, I believe there is a version of Bison (or maybe YACC) for Java. I believe it is JavaCC.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Complicated for loop's

    Quote Originally Posted by jim829 View Post
    Yep! In fact, I believe there is a version of Bison (or maybe YACC) for Java. I believe it is JavaCC.
    Don't forget ANTLR; it can parse LL* grammars (LL grammars with infinite look-ahead; very powerful and fast); of course it can also generate its own lexical analyzer.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Complicated for loop's

    Since I very rarely write parsers I didn't know about ANTLR. That's what I really like about this forum. I am always learning stuff.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Complicated for loop's

    Quote Originally Posted by jim829 View Post
    Since I very rarely write parsers I didn't know about ANTLR. That's what I really like about this forum. I am always learning stuff.
    You should give it a try once; for one thing, I love the error messages you can synthesize with it (LL(k) allows for much clearer error messages and recovery than LALR(k) can). It's LL* mechanism is ingenious.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Complicated for loop's and array checking
    By JordiLaForge in forum New To Java
    Replies: 1
    Last Post: 05-19-2013, 09:23 PM
  2. Complicated problem between classes
    By uhertz in forum New To Java
    Replies: 3
    Last Post: 06-20-2011, 01:58 AM
  3. replacement for complicated if else?
    By TopNFalvors in forum New To Java
    Replies: 12
    Last Post: 04-09-2011, 07:05 PM
  4. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  5. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 11:31 AM

Posting Permissions

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