Results 1 to 6 of 6
Thread: Java ERROR
- 04-11-2010, 08:02 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Java ERROR
Hey Everyone,
I'm doing an assignment for my computer science course and I got stuck on an error and just thought you might make it clearer for me, i'm not asking that you do anything more than make it clearer, although i appreciate any extra help,
this is my code:
and these are the errors I get:Java Code:import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Parser { private boolean success; private Parser.Scanner scanner; private Parser() { } public Parser(String paramString) { this.success = false; try { this.scanner = new Parser.Scanner(paramString); } catch (FileNotFoundException localFileNotFoundException) { System.out.println("Unable to find file " + paramString); return; } while ((localLexeme = this.scanner.getNextLexeme()) != null) { Lexeme localLexeme; System.out.print("The scanner found a lexeme with source code \"" + localLexeme.getSource() + "\" at line "); System.out.println(localLexeme.getLineNumber() + " and assigned token " + localLexeme.getToken().name()); } this.success = true; } public boolean isSuccessful() { return this.success; } private static class Scanner { private Scanner javaScanner; private Pattern pattern; private Matcher matcher; private String sourceLine; private int lineNumber; private Scanner() { } public Scanner(String paramString) throws FileNotFoundException { this.sourceLine = ""; this.lineNumber = 0; this.javaScanner = new Scanner(new File(paramString)); } public Lexeme getNextLexeme() { if (this.sourceLine.length() == 0) { if (!this.javaScanner.hasNextLine()) { return null; } this.sourceLine = this.javaScanner.nextLine(); this.lineNumber += 1; } this.pattern = Pattern.compile("\\s+"); this.matcher = this.pattern.matcher(this.sourceLine); if (this.matcher.lookingAt()) { this.sourceLine = this.sourceLine.substring(this.matcher.group().length()); } for (Lexeme.Token localToken : Lexeme.Token.values()) { this.pattern = Pattern.compile(localToken.getRegex()); this.matcher = this.pattern.matcher(this.sourceLine); if (!this.matcher.lookingAt()) continue; this.sourceLine = this.sourceLine.substring(this.matcher.group().length()); return new Lexeme(this.matcher.group(), this.lineNumber, localToken); } return null; } } }
I belive this is very easy, but i don't know why i'm not getting it!Java Code:Parser.java:30: cannot find symbol symbol : variable localLexeme location: class Parser while ((localLexeme = this.scanner.getNextLexeme()) !=null) Parser.java:62: cannot find symbol symbol : constructor Scanner(java.io.File) location: class Parser.Scanner this.javaScanner = new Scanner(new File(paramString)); Parser.java:69: cannot find symbol symbol : method hasNextLine() location: class Parser.Scanner this.sourceLine = this.javaScanner.hasNextLine();
thanx to who ever helps
- 04-11-2010, 11:07 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
You defined your localLexeme variable in the body of your while loop but you are trying to use it already in the while(<condition>) line which is outside of the body of the while loop; so do this instead:
... or if you really want to go fancy you can do this:Java Code:Lexeme localLexeme; while ((localLexeme = this.scanner.getNextLexeme()) != null) { // ... }
About that Scanner class: rename it because inside of that class when you simply define 'Scanner' the compiler thinks you're talking about your own Scanner class, not the java.util.Scanner class.Java Code:for (Lexeme localLexeme; (localLexeme = this.scanner.getNextLexeme()) != null; ) { // ... }
kind regards,
Jos
- 04-12-2010, 03:15 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Thank You so much for your help, the first tip worked very well, i don't know how i missed that.
"About that Scanner class: rename it because inside of that class when you simply define 'Scanner' the compiler thinks you're talking about your own Scanner class, not the java.util.Scanner class."
I'm so confused now, since i have too many scanners, i'll try to figure it out, but if you can help i'll be very thankful ;)
I hope other users can help with the other problems
Thanx
- 04-12-2010, 08:10 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
- 04-12-2010, 01:21 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
thanks for info, helpful for me...
- 04-12-2010, 11:31 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Java Servlet : Error
By KumbhaniMehul in forum Java ServletReplies: 0Last Post: 04-07-2010, 06:15 AM -
Error msg in java
By divyas in forum New To JavaReplies: 1Last Post: 01-28-2010, 09:37 AM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Please Help new to Java and keep getting error
By CloseQuarters in forum New To JavaReplies: 1Last Post: 12-18-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks