Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java error

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    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:

    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
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Java error

    Well hi d7o0om,

    Im a beginner too but I think I know a bit to help you.

    Your first error:
    you use the variable localLexeme in a while() loop while you declare it inside the while loop, this will not work that way. The solution is simple, put the declaration before the while() loop.

    Your second error:
    Im not sure, haven't played with the IO yet, but try looking into the "new File(paramString)" part, try declaring it into a variable and then put it in the constructor of the Scanner(). Try debugging from there, look what type the new variable will be and it should be java.io.File else its wrong.

    Your third error:
    Look into the Parser.Scanner class and check if the class has the method hasNextLine(), and be specific because hasNextLine() is not the same as hasNextLine(param). The method must not have arguments.

    Good luck!

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java error

    Thank You so much for your help, the first tip worked very well, i don't know how i missed that.

    I hope other users can help with the other problems

    Thanx

  4. #4
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Java error

    You should be able too check the third error, see what methods there is.

Similar Threads

  1. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  2. java.lang.numberformatexception error
    By natalie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2010, 04:16 AM
  3. Java heap space error
    By Loki in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 27th, 2010, 10:47 PM
  4. an error message while runnung java
    By sravan_kumar343 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 25th, 2010, 10:19 AM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM