Results 1 to 6 of 6

Thread: Class Validator

  1. #1
    luizmeme is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Class Validator

    I'm attempting to code a program that validates a file named as Class.txt, the contents of the file are: class test {
    int i;
    }

    the thing is, I can't get past the first two words, class and test. Can anyone help me?

    XML Code:
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Parser {
        
        private FileReader content;
        private int currentLineNumber = 1;
        private int currentCharNumber = 0;
        private int currentValue;
        private char currentChar;
        private String token;
        
        public static void main( String[] args ) {
            try {
                new Parser( new File( "C:/Class.txt" ) ).parse();
            } catch ( ParseException exc ) {
                exc.printStackTrace();
            } catch ( IOException exc ) {
                exc.printStackTrace();
            }
        }
        
        public Parser( File file ) throws IOException {
            content = new FileReader( file );
        }
        
        public void parse() throws ParseException, IOException {
            
            nextToken();
            
            if ( token.equals( "class" ) ) {
                System.out.println( token );
                nextToken();
            } else {
                throw new ParseException( token, currentLineNumber, currentCharNumber );
            }
            
            identifier();
            
            if ( token.equals( "{" ) ) {
                System.out.println( token );
                nextToken();
            } else {
                throw new ParseException( token, currentLineNumber, currentCharNumber );
            }
            
            type();
            identifier();
              
            if ( token.equals( "}" ) ) {
                System.out.println( token );
                nextToken();
            } else {
                throw new ParseException( token, currentLineNumber, currentCharNumber );
            }
            
            if ( token == null ) {
                System.out.println( "file syntactically correct." );
            }
            
        }
        
        private void type() throws ParseException, IOException {
            
            if ( token.equals( "int" ) || token.equals( "string" ) || token.equals( "char" ) ) {
                System.out.println( token );
                nextToken();
            } else {
                throw new ParseException( token, currentLineNumber, currentCharNumber );
            }
            
        }
        
        private void identifier() throws ParseException, IOException {
            
            if ( token.matches( "^[a-zA-Z]+[a-zA-Z0-9]*" ) ) {
                System.out.println( token );
                nextToken();
            } else {
                throw new ParseException( token, currentLineNumber, currentCharNumber );
            }
            
        }
        
        private void nextToken() throws IOException {
            
            token = null;
                
            while ( ( currentValue = content.read() ) != -1 ) {
    
                currentChar = (char) currentValue;
    
                if ( currentChar != ' ' && currentChar != '\n' && currentChar != ';' ) {
                    if ( token != null ) {
                        token += currentChar;
                    } else {
                        token = String.valueOf( currentChar );
                    }
                    currentCharNumber++;
                } else {
                    if ( currentChar == ' ' ) {
                        currentCharNumber++;
                        break;
                    } else if ( currentChar == '\n' ) {
                        currentLineNumber++;
                        currentCharNumber = 1;
                        break;
                    }
                }
    
            }
            
        }
        
    }
    
    public class ParseException extends Exception {
        
        private String token;
        private int line;
        private int Char;
        
        public ParseException( String token, int line, int Char ) {
            this.token = token;
            this.line = line;
            this.Char = Char;
        }
    
        @Override
        public String toString() {
            return String.format( 
                    "Error \"%s\" (line: %d, Char: %d)", 
                    token, line, Char );
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Class Validator

    Have you tried debugging your code to see what it is doing?
    If you do not have an interactive debugger, then add println statements to the code to print out the values of variables as they change and to show the execution flow. The printed output should help you understand what the program is doing.

  3. #3
    luizmeme is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Class Validator

    this is what the output shows when I execute the code:

    class
    test
    Error "{" (line: 2, Char: 1)
    at parser.Parser.parse(Parser.java:51)
    at parser.Parser.main(Parser.java:22)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Class Validator

    That message is from your code. Why does your code throw that exception? What are the values of all the variables that cause that exception to be thrown? Add some more printlns to see the variables' values.

  5. #5
    luizmeme is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Class Validator

    It's supposed to read the file class.txt.

    The contents of the class are:
    class test {
    int i;
    }

    I have added a println before this condition:
    if ( token.equals( "{" ) ) {
    System.out.println( token );
    nextToken();
    } else {
    throw new ParseException( token, currentLineNumber, currentCharNumber );
    }

    the content of the token is "{" but it still throws an exception and I couldn't figure out why.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Class Validator

    Reread posts#2 and #4 for suggestions on how you can find the problem.

    Here's a technique you should use when printing variable values: Add delimiters:
    S.o.p("var=" + var + "<");

    Then you can see if there are extra spaces or if the variable is empty.
    Last edited by Norm; 12-10-2011 at 07:17 PM. Reason: Added technique

Similar Threads

  1. HELP: SQL Query Validator
    By rjuyal in forum JDBC
    Replies: 1
    Last Post: 05-05-2008, 09:13 AM
  2. sql validator
    By Joe2003 in forum Advanced Java
    Replies: 0
    Last Post: 02-07-2008, 06:23 PM
  3. Commons validator
    By aczuczor in forum Web Frameworks
    Replies: 0
    Last Post: 07-27-2007, 09:46 AM
  4. Replies: 1
    Last Post: 07-06-2007, 05:39 PM
  5. Validator framework
    By Daniel in forum Web Frameworks
    Replies: 1
    Last Post: 06-25-2007, 06:29 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
  •