Array of Objects compiler error

December 15, 2010 at 11:15:17
Specs: Windows XP
Hi, I am trying to create an array of objects to store names and telephone numbers. So far it's gone ok but my compiler is throwing errors all over the place. I have 2 files the ArrayList.java file and ArrayListTest.java which contains my main method. However I cannot seem to find why the complier is throwing these errors at me. My code is included below:

ArrayList.java

public class ArrayList
{
	String name;
	String telNo;
	public void TelEntry()
	{
		name = "";
		telNo = "";
	}
	
	public String getName()
	{
		return name;
	}
	public String getNumber()
	{
		return telNo;
	}
	public void setName(String newName)
	{
		name = newName;
	}
	public void setNumber(String newTelNo)
	{
		telNo = newTelNo;
	}
}

ArrayListTest.java:

public class ArrayListTest
{
	public static void main(String[] args)
	{
		ArrayList[] entries = new ArrayList[3];
		
		entries[0] = new ArrayList("Tom","123456");
		entries[1] = new ArrayList("Mark","654321");
		entries[2] = new ArrayList("Jack","987654");
		
		System.out.printf("%6f%\t%6f", entries[0]);
		System.out.printf("%6f%\t%6f", entries[1]);
		System.out.printf("%6f%\t%6f", entries[2]);
	}
}

Thanks all.


See More: Array of Objects compiler error

Report •


#1
December 15, 2010 at 13:58:53
You create entries[0], etc. with a constructor with two String values, but you haven't defined such a constructor in ArrayList.java. Fix that and the code will compile; but it won't run. You are hopelessly confused when printing out the values of entries[]. You are using printf, which formats numerical values, but the values that you wish to print are strings. I'll leave you to correct that one.

Report •

#2
December 15, 2010 at 15:27:56
Thanks alot :) got it working now after a bit of trail and error. Just wondering if you could check the code because I know it complies and outputs what I want but I'm not sure if the code is right the code I have now is:

public class ArrayListTest
{
	public static void main(String[] args)
	{
		ArrayList[] entries = new ArrayList[3];
		
		entries[0] = new ArrayList();
		entries[0].setName("Tom");
		entries[0].setNumber("123456");
		
		entries[1] = new ArrayList();
		entries[1].setName("Mark");
		entries[1].setNumber("654321");
		
		entries[2] = new ArrayList();
		entries[2].setName("Jack");
		entries[2].setNumber("987654");
		
		int i = 0;
		
		for(int count = 0; count < 3; count++)
		{
			System.out.println(entries[i].getName() + " " + entries[i].getNumber());
			i++;
		}
	}
}

if you could give me a heads up on whether thats correct code or not that would be great.

Thanks again


Report •

#3
December 16, 2010 at 03:50:31
That's certainly one way of doing it. More elegant would be to create a constructor as it avoids repeated code. Something like (in the ArrayList clss):

public ArrayList(String n, string t)
{
name = n;
telNo = t;
}

then you can use your original new ArrayList("Tom", "123456") calls.


Report •

Related Solutions


Ask Question