Java Gods?
Page 1 of 1
[sYn]
[Moderator] Elitist



Posts: 8374

PostPosted: Sun, 8th Jan 2006 00:16    Post subject: Java Gods?
I'm in a bit of a pickle, and if I remember correctly we have some Java god's on the forum, so lets hope you can help me out. I have to write a polymorphic java program to display images, move them, change them and pull settings from a file. Now I have a lot of the ground work done, but I always have issues with pulling information from files..

Lets say I have a file with the following information in it:

Shape: Circle, Position: ***, ***, Size: ***, ***, Colour: ***, Fill: ***

The stars obviously representing values like numbers, or colours.. How would I pull that into my program form a file? currently I have the following code which pulls in data one line at a time..

Code:
public boolean load(String file1)
   {
      boolean b = false;                                    // Dummy - Unused
      try
      {
         FileReader file = new FileReader("default.lst");         // Opens file "default.lst" or ignores if unavalible
         BufferedReader in  = new BufferedReader(file);
         int i = 1;
         String line = null;
         while((line = in.readLine()) != null)                   // Sets up and "end of file" while loop scanning each line
         {                                               // and putting it into a varliable until there are no more lines
            switch(i)
            {
            case 1 : contact[position][0] = line;break;            // each line is inputted into the array
            case 2 : contact[position][1] = line;break;
            case 3 :
               contact[position][2] = line;
               position++;                                 // position is incremented so the whole file is loaded line by line
               i=0;
               break;
            }
         
         i++;                                          // counter is incremented
         }
         in.close();                                       // Buffer is closed
      }
      catch (Exception e) {                                     // Exception is caught
         System.out.println(e);
      }
      totalposition = position;
      return b;                                          // Dummy - Unused
   }



Thoughts? Any idea's would be great Very Happy! - Oh, and I only have a few days.. so quick help would be awsome Very Happy..
Back to top
Avenger_




Posts: 658
Location: Norway
PostPosted: Sun, 8th Jan 2006 02:53    Post subject:
File-reading is always a hassle, and this is not only in java. Smile
I would suggest something like splitting first the line on every ','-character and then, split it again on ':'. this way you could read the data and the type separately, and it would not need to have a specific order in the file. One way to check for errors is to check how many times the line can be split, and if each segment can be split by ':' again. Also use some sort of search and replace to remove any spaces in the fields.

I'm not a java-programmer so this would just be a standard way to read it.
Back to top
tRanSwarP




Posts: 202
Location: Germany
PostPosted: Sun, 8th Jan 2006 14:14    Post subject:
Avenger is right. That sounds like a good way.

For the splitting thing have a look at the StringTokenizer class (java.io or java.util methinks).
This class does all this splitting work.
Back to top
sabalasa




Posts: 369
Location: EST
PostPosted: Sun, 8th Jan 2006 21:33    Post subject:
if you have the conf files in your classloader path (classloader path is the compilation root dir ie. in the tree com/foo/bar/main.class the root dir is the dir with the com folder) you can use the classloader's input stream to load configuration files. Even better, if your configuration is key-value based then you can use the Java Properties object to automatically load the key-value pairs. Properties loader even supports comments (#).

The properties file syntax is simple: key=value. For example circle.properties would contain the following data:
Code:

position=x,y
size=a,b
colour=white
fill=solid


this way you define properties files for all shapes in different files ie sqare.properties, triangle properties etc.

And then you create a super class named Shape

Code:

public abstract class Shape {
    protected java.util.Properties env;

    public Shape() {
        env = new Properties();
    }
    protected String[] parseValue(String value) {
        return (value.split(",")); //splits the value into string array using "," as the delimiter
    }
    public abstract void draw();
}


and you just subclass it

Code:

public class Circle extends Shape {
    public Circle() {
        super();
        java.io.InputStream in = Circle.class.getClassLoader().getResourceAsStream("circle.properties");
       env.load(in);
    }

    public void draw() {
        .
        .
        .
    }
}


You do the same with all shapes and end every time you instantiate a shape you load the shape specific info into the env property. Then you must implement the configuration methods for all shapes (you can define it in the abstract class if it's the same with all shapes or use polymorphism and define the abstract methon in the abstract class and the method body in every shape class)

If you cannot change the conf file layout then you'll have to implement it using either a StringTokenizer or a StringBuffer object but that's more like doing a brain surgery through the patient's anus Razz

hope it helps


rgds
Sabalasa
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group