Results 1 to 6 of 6
Thread: Class Validator
- 12-10-2011, 04:31 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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 ); } }
- 12-10-2011, 05:45 PM #2
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.
- 12-10-2011, 06:03 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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)
- 12-10-2011, 06:05 PM #4
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.
- 12-10-2011, 06:12 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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.
- 12-10-2011, 06:13 PM #6
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 06:17 PM. Reason: Added technique
Similar Threads
-
HELP: SQL Query Validator
By rjuyal in forum JDBCReplies: 1Last Post: 05-05-2008, 08:13 AM -
sql validator
By Joe2003 in forum Advanced JavaReplies: 0Last Post: 02-07-2008, 05:23 PM -
Commons validator
By aczuczor in forum Web FrameworksReplies: 0Last Post: 07-27-2007, 08:46 AM -
Struts Validator - JScript version of Validator ´validwhen´
By Felissa in forum Web FrameworksReplies: 1Last Post: 07-06-2007, 04:39 PM -
Validator framework
By Daniel in forum Web FrameworksReplies: 1Last Post: 06-25-2007, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks