[[email protected]]
[[email protected], [email protected]]
[[email protected], [email protected], [email protected]]
i believe this is showing the memory location of the object, and i have noticed each new value in turn overwrites the previous, could some please explain why it is doing this and what code i should use to overcome this
import java.util.*;
import java.io.*;
abstract class Animal {
static String name;
static String cname;
String noise;
static ArrayList<Animal> al= new ArrayList<Animal>(); ;
public Animal(String n)// seperaate constructor
{
name = n;
}
public static void main (String[] args)throws IOException
{
Dog d = new Dog("fido");
System.out.print(d.name);
//System.out.println(d.name);
Cat c = new Cat("buju");
System.out.println(" "+ d.get_noise());
;
FileReader fr = new FileReader("cat.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine())!=null)
{
if (line.startsWith("Cat"))
{
String name1 = line.substring(line.indexOf("Cat")+4, line.length());
//Cat c = new Cat(name1);
//al.add(new Cat(name1));
Animal n = new Cat(name1);
al.add(n);
System.out.println(al);
}
}
/*for (Animal p :al)
{
System.out.println(p.name +" "+p.noise);
}*/
}
/*public String toString()
{
return name +" " + noise;
}*/
public abstract String get_noise();
}

New Topic/Question
Reply



MultiQuote



|