2 Replies - 195 Views - Last Post: 06 December 2017 - 04:41 AM Rate Topic: -----

#1 LoGdOg9191  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-October 17

Trouble using ArrayList

Posted 05 December 2017 - 09:22 PM

I'm supposed to write a class named PhoneBookEntry that has fields for name and phone number, and write a program that creates five PhoneBookEntry objects and stores them in an ArrayList. Then it's supposed to use a loop to display the contents of each object.

My issue is that in each instantiation of my five objects I get a cannot find symbol error like so:


error: cannot find symbol
PhoneBookEntry entry1 = phoneList.new PhoneBookEntry();
^

I've tried everything I can thing of and can't seem to figure out the cause of my issue. I feel like it's something simple that I'm just overlooking but I honestly don't know. Any help would be greatly appreciated.

My code is as follows:

public class PhoneBookEntry
{	
		//declarations 
		private String name;
		private int phoneNumber;
		
		
		public PhoneBookEntry ()
		{
			
		}
		
		public void SetName(String name) 
		{
			this.name = name;
		}
		
		public String getName()
		{
			return name;
		}
		
		public void setPhoneNumber(int phoneNumber)
		{
			this.phoneNumber = phoneNumber;
		}
		
		public int getPhoneNumber()
		{
			return phoneNumber;
		}
}







import java.util.ArrayList; // for array list use

public class PhoneBookEntryProgram
{
	
	
	public static void main(String[] args)
	{
		
		
		
		ArrayList<PhoneBookEntry> phoneList = new ArrayList<>();	
		
		//create objects
		PhoneBookEntry entry1 = phoneList.new PhoneBookEntry();
		entry1.setName("Curtis");
		entry1.setNumber(8286761111L);
		phoneList.add(entry1);
		
		PhoneBookEntry entry2 = phoneList.new PhoneBookEntry();
		entry2.setName("Logan");
		entry2.setNumber(8286762222L);
		phoneList.add(entry2);
		
		PhoneBookEntry entry3 = phoneList.new PhoneBookEntry();
		entry3.setName("Jaron");
		entry3.setNumber(8286763333L);
		phoneList.add(entry3);
		
		PhoneBookEntry entry4 = phoneList.new PhoneBookEntry();
		entry4.setName("Deane");
		entry4.setNumber(8286764444L);
		phoneList.add(entry4);
		
		PhoneBookEntry entry5 = phoneList.new PhoneBookEntry();
		entry5.setName("Ashley");
		entry5.setNumber(8286765555L);
		phoneList.add(entry5);
		
		
		
		
		
		
		
		
		//for loop to output the list
		for (int i = 0; i < phoneList.size(); i++)
		{
			System.out.println(phoneList.get(i));
		}
		
		
	}
}




I'm not sure if my output is correct because I haven't gotten it to compile yet.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble using ArrayList

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 788
  • View blog
  • Posts: 3,206
  • Joined: 13-June 14

Re: Trouble using ArrayList

Posted 05 December 2017 - 11:40 PM

You shouldn't have phoneList. on those lines. That just isn't correct Java syntax - creating a PhoneBookEntry instance has nothing to do with your ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Trouble using ArrayList

Posted 06 December 2017 - 04:41 AM

Quote

entry1.setNumber(8286761111L);

A phone number is a string, not a number. If it were legitimately int (it isn't) then you wouldn't assign long to it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1