I want to create a class which will be responsible for reading an input file and then filling up my other classes...But i do not know how to begin...any ideas?

Recommended Answers

All 15 Replies

Not much to go on. What sort of input file are you reading? Text? Source code? CSV? XML? Something else?

Where are you stuck? Do you know how to open and read the file? If not, try google, there's a lot to help you there. Then you have to read the contents. While you're not at the end of the file, get a line and do stuff with what you find. Repeat.
What stuff you do depends on the answer to the first question.

What code do you have so far?

Well you are rigth i should have been more specific.Look its a .txt file, which contains the components of an object-oriented program..say all the packages in it,all the classes in each package alla methods in each class etc..I have also created a Model-package other than the parser.In which for each class i read from my text file i should save its name i an class object which is a composition of a method object for all the methods in this class..What i dont get is how my parser,having read a line from the text file,will fill up those objects..since i dont actually get what i am supposed to do i havent written any code yet..

Maybe you should give us a sample of the text file you're trying to read, and some idea of what it's supposed to result in.
It sounds like you want to compile java, but I'm pretty sure you wouldn't have wandered into a compilers course by accident.

no i dont need to compile anything...see i have this input file..my parser should log in this and then for every class i read in i shoud create a class-object and for every method in this class i should create a method-oblect...

add   (Unknown Method)

arraycopy   (Unknown Method)

Attribute   (Unknown Class)

Basic_Algorithms   (Package)
   [Combinations.java, 1]

Basic_Algorithms.Combinations   (Class)
   [Combinations.java, 3]

Basic_Algorithms.Intersection   (Class)
   [Intersection.java, 8]

Basic_Algorithms.Intersection.Find   (Method)
   [Intersection.java, 11]

Basic_Algorithms.Intersection.Find.(for_loop_1).i   (Variable)
   [Intersection.java, 13]

Basic_Algorithms.Intersection.Find.(for_loop_2).i   (Variable)
   [Intersection.java, 16]

And your Class class (there's already a class called Class, this could cause complications) wants to have a collection of Method objects, and a Method has a collection of Variables? Am I understanding you correctly now? You're making an in-memory representation of a bunch of class files?

yeap that's exactly what this program is about..so far i have made the architecture of my program class and sequence diagrams..

Okay, so now we're on the same page. Cool.
If you want to parse that text, you need to know how you're going to recognize a Class, a Method, a Package, and a Variable. I'm a little concerned about the possibility of inner classes, those would complicate things a little.

suppose I have two classes, and one of them has an inner class, as follows:

public class ClassOne
{
  public static void main(String[] args)
  {
    int i = [B]ClassTwo.InnerClass.getValue()[/B];
  }
}
public class ClassTwo
{
  	public static class InnerClass
	{
		public static final int value= 2;
		public static int getValue()
		{
			return value;
		}
	}
}

You see how the bolded line makes life a little interesting? It might be a little more clear if you realize that there could be a package name on that as well.
There are some tricks to deal with this, of course, but the question is whether that's part of the assignment.

In your text file, you're getting some fully-qualified names, with the hierarchy package.class.method.variable. If that's a consistent convention, then it's pretty easy.

But how do you distinguish between these?

add   (Unknown Method)
arraycopy   (Unknown Method)
Attribute   (Unknown Class)
Basic_Algorithms   (Package)

What is it that tells us that add and arraycopy are methods, Attribute is a class, and Basic_Algorithms is a package?

well firstly no this input file doesnt get to have a class included in a class.now the hierarchy u mentioned is consistent i mean when i get a method its format will be like this: package_name.method_name (method) package_name.method_name.variable_name (variable) whatever is marked as unknown is a method in java..so dont care about it..what i dont get is when ever i parse a line like this //package_name.method_name.variable_name// what i am supposed to do afterwards...

what i dont get is when ever i parse a line like this //package_name.method_name.variable_name// what i am supposed to do afterwards...

I don't suppose you'd have that string, would you? You'd have package_name.class_name.method_name.variable_name, right?
Well, you need to break that up into four Strings, at the '.' character. The String class has a method for this, it's called split() - take a look at the API for String, if you haven't used this already.
That'll give you an array of Strings. If you know that the first is package, second is class, and so forth, you've pretty much got it in your pocket at that point.

I have strings in the format i told you.. package_name.class_name.method_name.variable_name (variable)
package_name.class_name.method_name(method)

now say i want to create a package-object with package_name which is a composition of class-objects and name a class-object with the class_name that I will get.how will i do this? these items will be created by this parser and then name it properly?

If you have both of those formats, then you can't just use position in the string. You're going to need something to tell you what's a package and what's a class and so forth.

In a java source file, you have a declaration. You know a given label marks a package, because it says "package", and so forth. If you have that information - some sort of declaration, somewhere in this file - then you can use a two-pass process. The first pass reads through the data and finds declarations, the second pass then uses those declarations to process the rest of the information.

If you don't have absolute position to work with, and you don't have declarations, I'm afraid you might be stuck. I don't see how you'd do it without one of those two.

well i have a proper declaration of what is what i mean that whenever i read i line of say for example a class this is what i will get

Basic_Algorithms.Intersection   (Class)
   [Intersection.java, 8]

so when i find the class mark i will read one time after the "." mark and so on..i think this will work.But let me see if i understand what your suggesting..I parse the whole file only for packages and then create all the package objects i will need and then fill all the classes a specific package may have?

So your incoming format tells you what the last element of a qualified name is. In your example, we know that there is a class called Intersection, and it's contained in Basic_Algorithms. We can further deduce that Basic_Algorithms is a package, because there is no other object in the hierarchy that can contain a class (now that you've outlawed inner classes). That's a deduction, you'll notice, that you'd have to program into your parser, it's not obvious to the machine.

Maybe I'm just not understanding what you're trying to do, but this seems a really bizarre way to represent this information. What is it you're after doing, anyway? What's all this in aid of?

Well look this input is actually all the componets of an object-oriented system.My program is supposed to read it and then provide to the user information about it.So that he can be able to project a class or a method and i should also be able to produce an output file which will be an input to violet.So what i asked before was it correct? should i first create all the package-objects then all the class-methods and so on?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.