Solved

Why doesn't return value?

Posted on 1998-03-14
6
180 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

762 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

18 Experts available now in Live!

Get 1:1 Help Now