Results 1 to 6 of 6

Thread: Java ERROR

  1. #1
    d7om is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default 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:

    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;
        }
      }
    }
    and these are the errors I get:

    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();
    I belive this is very easy, but i don't know why i'm not getting it!


    thanx to who ever helps

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

    Default

    Quote Originally Posted by d7om View Post
    Java Code:
        while ((localLexeme = this.scanner.getNextLexeme()) != null)
        {
          Lexeme localLexeme;
          // ...
        }
    and these are the errors I get:

    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();
    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:

    Java Code:
        Lexeme localLexeme;
        while ((localLexeme = this.scanner.getNextLexeme()) != null)
        {
          // ...
        }
    ... or if you really want to go fancy you can do this:

    Java Code:
        for (Lexeme localLexeme; (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.

    kind regards,

    Jos

  3. #3
    d7om is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    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

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

    Default

    Quote Originally Posted by d7om View Post
    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
    Basically the structure of your code is like this:

    Java Code:
    public class Parser {
       ...
       private static class Scanner { ... }
    }
    My advice was and still is that you rename that Scanner class.

    kind regards,

    Jos

  5. #5
    concer is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks for info, helpful for me...

  6. #6
    d7om is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanx for your help. this helped and it is now working properly


    thanx

Similar Threads

  1. Java Servlet : Error
    By KumbhaniMehul in forum Java Servlet
    Replies: 0
    Last Post: 04-07-2010, 06:15 AM
  2. Error msg in java
    By divyas in forum New To Java
    Replies: 1
    Last Post: 01-28-2010, 09:37 AM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Please Help new to Java and keep getting error
    By CloseQuarters in forum New To Java
    Replies: 1
    Last Post: 12-18-2008, 07:39 PM

Posting Permissions

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