Solved

Why doesn't return value?

Posted on 1998-03-14
6
192 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now