Solved

Why doesn't return value?

Posted on 1998-03-14
6
225 Views
Last Modified: 2010-04-16
I have a problem here. I have written 2 programs - one is to read input data from a text file using StreamTokenizer and pass the input to the other program which is the data structure. Somehow I could not get my data structure to print out the input. Could you please check whether I did my programming correctly?

This is my first program called Parser.java in which I wrote this program based on this grammar:
      parse_file -> parse_item  parse_file
      parse_item -> pItem | lItem | rItem
      pItem -> POINTS  object  attlist  pbody
      object -> STRING

I'm giving you part of my program here:
      
Vector parse_file() throws IOException, ParserException {
            Vector file = new Vector();

            parse_item(file);
            int token = stok.nextToken();
            if (!NextStringToken("end")) {
                  if (token == stok.TT_WORD) {      
                        return (this.parse_file());
                  }
            }
            return file;
      }

      SpatialCategory parse_item(Vector file) throws IOException, ParserException {
            if (NextStringToken("POINTS")) {
                  return (pItem(file));
            }
            return (pItem(file));
      }

      PointCategory pItem(Vector file) throws IOException, ParserException {
            PointCategory sc = new PointCategory();
            file.addElement(sc);

            this.MatchString("POINTS");
            object(sc);
            attlist(sc);
            pbody(sc);

            return sc;
      }
      
      SpatialCategory object(SpatialCategory sc) throws IOException, ParserException {
            int tok;
            if ((tok = stok.nextToken()) != stok.TT_WORD) {
                  throw new ParserException("Object is not word!");
            }
            else {
                  System.out.println("Object: " + stok.sval);
                  sc.name = stok.sval;
            }
            return sc;
      }

My input file:(for this part of program)
      POINTS
      postoffice
When I ran this program, it could read the value postoffice
and printed it out.

This is my data structure program - SpatialCategory.java
public class SpatialCategory extends Applet {
      String name;
      Vector atts = new Vector();

      public void paint(Graphics screen) {
            System.out.println(" " + name);
      }
}

class PointCategory extends SpatialCategory {
      Vector pIndividuals = new Vector();
                  
}

When I ran this program, it will printed out the value name as null in which it should be postoffice.

Can you please help me.
0
Comment
Question by:suhani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1233554
I'm not quite clear on why you would expect different behaviour.
SpatialCategory is a Java program. It is independent of (i.e.
knows nothing of) any other Java, C, C++ or any other code or
program in the system.
The variable name is declared as a String, but nothing is ever
assigned to it. Therefore its value is null.

Did I miss something? Was there something not posted or misposted?

0
 

Author Comment

by:suhani
ID: 1233555
Then how do I connect between these two programs?
0
 
LVL 16

Accepted Solution

by:
imladris earned 30 total points
ID: 1233556
The simplest solution, I think, is to combine them into a single program. Parser becomes part of the initialization of the whole program. The data it has read in, is then available to the program in some convenient form.

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:suhani
ID: 1233557
But when I combined these two programs, it would not run. Could you please tell me why?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1233558
I'd be happy to try, but you'll have to be more specific. There are millions of things that could go wrong. Is the compiler complaining? Are you getting an exception? Is it producing the wrong results? Include some relevant code.

0
 

Author Comment

by:suhani
ID: 1233559
It's ok now. Thank you.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question