Need help with vector lists and reading from input files

Associate
Joined
21 May 2003
Posts
1,008
Hi there. I'm trying to make some code for shapes and colours.
Code:
public static Vector<Material> materials = new Vector<Material>();
Matte purple = new Matte();
		purple.set_cd(new RGBColour(0.5f, 0, 1));
		materials.add(purple);
so now I've got the colour purple in my materials vector. (Matte extends Material).

however, when I want to set the material of a sphere to purple (or any other colour, it won't work:
Code:
 objects.get(0).set_material((Material) line[1]);

line[1] is a buffered reader input and it's coming from an outside file. with *** above code it give the error:
inconvertible types, found string, required material.

but the function set_material requres a material type.

so how do i convert the String it's read from the file to a Material type?

i know i can do Integers and Doubles but what if it's not a standard java type?
 
but purple is already in the materials vector.
All i need to do is get the string and match that string with one of the values from the vector list. then i can get it's index.
 
Code:
String input;
			FileReader file = new FileReader("input.txt");
			BufferedReader reader =  new BufferedReader(file);
			
			while (( input = reader.readLine() ) != null){
				String[] line = input.split(" ");
example input:
Code:
Sphere purple 0 0 0 30

Code:
if (line[0].equals("Sphere")){
					System.out.println("Making Sphere from input file");
					add_object(new Sphere( line[0],new Point3D(Double.valueOf(line[2]),Double.valueOf(line[3]),Double.valueOf(line[4])), Double.valueOf(line[5]) ));
				
				}

that's what I originally had. where it would compain about the line[0] part in the sphere construction (that it's a string, not a material). but there is a material in this class called purple, i just don't know how to make this thing recognise the actual value of line[0].

if i change line[0] to purple, it works fine.
 
Well you'll need to write some code that takes a string as an input and returns a corresponding Material object. Java can't know that a string containing "purple" means a specific object of type Material.
 
The easiest way is probably to use a Hashtable<string,Material> rather than a list of materials. You can then lookup the string in the table to return the material.
 
I can see how the hashtable method will work but i feel there must be a better solution.

how can i take the string as an input and convert it to a material? all i need from the string is the characters so i don't see how this can lead to a material.
 
I can see how the hashtable method will work but i feel there must be a better solution.

how can i take the string as an input and convert it to a material? all i need from the string is the characters so i don't see how this can lead to a material.

It isn't possible to do this in the kind of way you are suggesting, so Hashtable really is probably the very best you can do!

To explain why what I think you are describing cannot work:

You have created a reference called "purple", which points to an object of type Material that has the properties of the colour purple. You have then added this object to a list. It is important to note that you have added the Object of type Matte, NOT the reference called "purple". So now you have an Object in a list that has no idea it was once referenced by a reference called "purple", hence it will be impossible to do a lookup using this name. Secondly, reference names are destroyed during compilation, so there is no chance you could ever lookup on reference name!
 
I understand now, you were trying to use the string "purple" from the input stream as the name of a variable you defined earlier, this won't work in Java. In something like PHP this would be fine because it allows you to things like use a string as a method name for example.

I also think a HashMap is your best bet because you need some form of lookup table so you can map the string 'purple' to an actual object. You are currently using a Vector which isn't suitable as you have no way of performing a lookup. So here is how I would do it:

Code:
HashMap<String,Material> materials = new HashMap<String,Material>();
Matte purple = new Matte();
purple.set_cd(new RGBColour(0.5f, 0, 1));
materials.put("purple", purple);

I've created a new HashMap to hold a String (as the key) and a Material (as the value). You then populate this HashMap with all the colours you will be using (just purple in the provided example). Next I added the new colour to the HashMap with the key which is a String that contains the word "purple".

To use the HashMap, assuming your previous code works you would do:

Code:
if (materials.containsKey(line[1])) {
	objects.get(0).set_material(materials.get(line[1]));
} else {
	// Handle the error condition here.
}

This part of the code is where the lookup takes place. We first check to see if the HashMap actually contains a key that matches the String you read from the input stream, if it does then we use the key to search the HashMap, if the String matches a key in the HashMap (it should do as we checked beforehand)then it return the resulting Material. You may want to do something different if the key isn't in the HashMap such as throw an exception (maybe the input file is formatted badly if the colour isn't found).

I hope this helps you out a bit.
 
Back
Top Bottom