Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why doesn't return value?

Posted on 1998-03-14
6
Medium Priority
?
229 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
  • 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 60 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

877 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