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.
 
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.
 
Back
Top Bottom