Path: csiph.com!aioe.org!.POSTED!not-for-mail From: omegajunior Newsgroups: comp.lang.java.programmer Subject: Re:hash map Date: Sun, 16 Apr 2017 17:11:07 -0400 (EDT) Organization: nntp.aioe.org Lines: 66 Message-ID: References: <9a86c6fb-ae40-4acd-902f-afff0ab60fd6@googlegroups.com> NNTP-Posting-Host: b9RTYXdLhrqpjhUn00ce9g.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org X-Newsreader: PiaoHong.Usenet.Client.Free:1.65 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:36450 pablo Wrote in message: > hi all i am trying to add to a hash map but i cant seem to do it could somebody look at the code and tell me why the hell i cant add the key and value > > import java.util.*; > /** > * The class Garage is used to hold data about a collection of customers. > * > * @author M250 module team > * @version 1.0 > */ > public class Garage > { > //garage creating an empty instance of map map > private Map Customer = new HashMap(); > > > public Garage() > { > Customer = new HashMap<>(); > } > > public void addCustomer(String Customer) > {" > Customer.Put(Andrew Smith, address 1 The High Street", area 1, "123ABC"); > } > 1. You seem to have got your structures wrong. What you're adding to the Customer map is , but instead of Address you've defined Customer to contain Customer. If that doesn't confuse you, I applaud your mental prowess. 2. Your code doesn't put a String and an Address into the map. That's because your code doesn't wrap the customer's name in quotes. Leaving out the quotes makes Java interpret the name as a Java language keyword or a class or variabla name, which probably isn't your intent. 3. Your code uses a Map#Put method. The Map class doesn't have a method named Put. It's named put. Note the lower-cased p. Combining that, this should work: final class mine { private final map customers = new HashMap<>(); public final void add() { this.customers.put("Andrew Smith", "1 The High Street, area 1, 123ABC"); } } If you want the customers map to hold an Address type rather than a String containing address data, you'll have to define an Address class, first. Does that help? -- We make software better. ----Android NewsGroup Reader---- http://usenet.sinaapp.com/