Solved

Why doesn't return value?

Posted on 1998-03-14
6
209 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 60
Chrome and Firefox Java 5 51
Notify sent to other threads in Java 9 33
hashmap order 17 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 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