Results 1 to 9 of 9

Thread: Syntax Errors

  1. #1
    andimiami is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Unhappy Syntax Errors

    Hi all! I'm SUPER new to Java, and pretty new to programming in general, so please forgive my awful coding. I'm practicing! I have to write a program that uses Scanner to lines in from a text file and use MatchResult to see if the lines match the regex. Basically, I need it to read random inputs, such as $5665% or 4445a and weed out only the ones the have exactly 5 word characters (A-Z a-z 1-9)...no special characters. This is what I have so far, but I can't play around with my regular expression because I seem to have some hideous syntax errors. I am using Eclipse and this is what its says:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error, insert "Finally" to complete BlockStatements
    Syntax error, insert "}" to complete MethodBody

    at ByteMonitor.main(ByteMonitor.java:40)

    I must be missing a curly brace, and on which block to put the Finally???


    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    import java.util.regex.*;
    
    public class ByteMonitor {
    
    	public static void main(String[] args) {
    		
    				
    	  try {
    			BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
    			String line = null; //corresponds to a string in the file
    			
    			      while((line = inputFile.readLine()) != null){	; //while there are lines left in the input file.
    			    	  
    			Scanner scan = new Scanner(line);
    			scan.findInLine ("(.*)(\\w {3})(.*)"); //find word or number characters of the length of 5
    			MatchResult result = scan.match();
    			if ( scan.hasNext() ){
    			      String name = scan.next();
    			      String value = scan.next();
    			      System.out.println ("OK") ;
    			    }
    			    else {
    			      System.out.println("FAIL");
    
    				    
    			      
    		   }
    			      scan.close();
    		            
    
    		   }
    			
    			//file I/O can potentially generate a FileNotFoundException if the specified file 
    		    //does not exist, and therefore must implement exception handling syntax
    
    			}catch (IOException e) { System.err.println("Error With File");}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Syntax Errors

    You need to format your code properly so that all curly braces line up and the code is uniformly indented correctly, usually 3-4 spaces will do. The reason you do this is not to make the code look pretty -- it's so you can match your braces and make sure they're right. it also makes it easier for volunteers to help you, and that's a good thing too.

  3. #3
    andimiami is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Syntax Errors

    Ok, I think I lined my code up better and actually found the elusive missing curly brace, but as I play around with the MatchResult, I seem to hae gotten myself hopelessly lost in what I really need to do here. After I run the:

    scan.findInLine ("[.*][\\w^_{5}][.*]"); //find word or number characters of the length of 5.
    MatchResult result = scan.match();

    What should I be doing to print out my "OK" and "FAIL"? I thought along the lines of an if/else like I would do in C++ possibly, but in Java, does that work as well? I am not sure what to put as my argument in the If () I want it to say in layman's terms:
    If (the scan.match is 5 word characters) Print "OK", Else Print "Fail". Any advice???
    Thanks!!!

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    import java.util.regex.*;
    
    public class ByteMonitor {
    
    	public static void main(String[] args) 
    {
    		
    				
    	  try {
    			BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
    			String line = null; //corresponds to a string in the file
    			
    				while((line = inputFile.readLine()) != null){	; //while there are lines left in the input file.
    			    	  
    			Scanner scan = new Scanner(line);
    			scan.findInLine ("[.*][\\w^_{5}][.*]"); //find word or number characters of the length of 5.
    			MatchResult result = scan.match();
    			
    			if ( scan.match() != null )
    				{
    			     System.out.println ("OK") ;
    			    }
    			else 
    				{
    			      System.out.println("FAIL");				    
    			      
    				}
    			      scan.close();            
    
    		   }
    			
    			//file I/O can potentially generate a FileNotFoundException if the specified file 
    		    //does not exist, and therefore must implement exception handling syntax
    
    			}catch (IOException e) { System.err.println("Error With File");}
    		}
    }

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Syntax Errors

    Also, error message states the line number where you get the error (syntax error).

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error, insert "Finally" to complete BlockStatements
    Syntax error, insert "}" to complete MethodBody

    at ByteMonitor.main(ByteMonitor.java:40)
    The error message tells you that the error is on ByteMonitor.java class in line 40

  5. #5
    andimiami is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Syntax Errors

    Thanks! I think I fixed all of those. Now I am having problems with actually getting a RegEx to work...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  7. #7
    andimiami is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Syntax Errors

    I can't get ANYTHING to match, no matter what I put as an argument. I commented out the if-else loop, and went with a for loop example we used in class. What the heck am I doing wrong??? SO frustrated!
    Java Code:
    //****************************************************Inputs*********************************************
    //	 	^&*(#@! (Explanation: To use all non-word characters)
    
    //	 	abcde   (Explanation: To all char characters)
    
    //	 	#       (Explanation: To use the shortest possible non-word character)
    
    //		a * B * c    (Explanation: To use whitespace between a combination of characters)
    
    //		anextremelylongstringofcharactersand1234s (Explanation: To use a fairly long length of line)
    
    //		1aaa0   (The rest were given im class)
    
    //		11111
    
    //		000
    
    //		*11jis
    
    //		12092*
    //************************************************************************************************************************
    
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    import java.util.regex.*;
    
    public class ByteMonitor {
    
    	public static void main(String[] args) 
    {
    		
    				
    	  try {
    			BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
    			String line = null; //corresponds to a string in the file
    			
    				while((line = inputFile.readLine()) != null){	; //while there are lines left in the input file.
    			    	  
    			Scanner scan = new Scanner(line);
    			scan.findInLine (""); //find word or number characters of the length of 5.
    			MatchResult result = scan.match();
    			for (int i=1; i<=result.groupCount(); i++)
    		                        System.out.println(result.group(i));
    
    			
    			/*if (result == scan.match())
    				{
    			     System.out.println ("OK") ;
    			    }
    			else 
    				{
    			      System.out.println("FAIL");				    
    			      
    				}*/
    			      scan.close();            
    
    		   }
    			
    			//file I/O can potentially generate a FileNotFoundException if the specified file 
    		    //does not exist, and therefore must implement exception handling syntax
    
    			}catch (IOException e) { System.err.println("Error With File");}
    		}
    }

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Syntax Errors

    First of all, do not edit your reply into another question, if am too lazy I will skip post#2 and post#7 (which are your reply) since I have already read those.
    ALSO, you might not receive any answer to your problem right now because we skip post#2 and 7. AND your thread will not moved to top because you just edit it.
    Aside from that, fubarable and my reply means non-sense now.

  9. #9
    andimiami is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Eclipse is making me go silly with it's syntax errors
    By magicmojo in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 02:48 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. A question about syntax.
    By mr_o in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 09:30 PM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. Cannot get passed these syntax errors
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 08:05 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
  •