Jump to content

Hi, I'm stuck again... and this time it's gonna be more frustrating than the last because I'm getting confused on which loop to be using for what purpose. I had help from some friends so... it got me that far. 

 

The method I'm working on // returns the number of winners in a given year (e.g. 2, in 1993). Should I be using keys with iterator? I just learnt that. 

 

 

So this is what my code looks like thus far:


import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Collection;

/**
 * This class describes the winners of the Nobel Prize in Peace between the years 1993 and 2009.
 * @author: Dominic Ng
 * @version: 1.0
 */
class NobelPrizeWinners{
    private HashMap<Integer, String[]> winner;

    /**
     * This is the first constructor which creates a HashMap that puts an array of Strings with winners associated by year. 
     */
    public NobelPrizeWinners(){
        winner = new HashMap<Integer, String[]>();

        winner.put(2009, new String[]{"Barack H. Obama"});
        winner.put(2008, new String[]{"Martti Ahtisaari"});
        winner.put(2007, new String[]{"Intergovernmental Panel on Climate Change (IPCC)","Albert Arnold (Al) Gore Jr."});
        winner.put(2006, new String[]{"Muhammad Yunus", "Grameen Bank"});
        winner.put(2005, new String[]{"International Atomic Energy Agency (IAEA)" , "Mohamed ElBaradei"});
        winner.put(2004, new String[]{"Wangari Muta Maathai"});
        winner.put(2003, new String[]{"Shirin Ebadi"});
        winner.put(2002, new String[]{"Jimmy Carter"});
        winner.put(2001, new String[]{"United Nations (U.N.)" , "Kofi Annan"});
        winner.put(2000, new String[]{"Kim Dae-jung"});
        winner.put(1999, new String[]{"Médecins Sans Frontières"});
        winner.put(1998, new String[]{"John Hume, David Trimble"});
        winner.put(1997, new String[]{"International Campaign to Ban Landmines (ICBL)" , "Jody Williams"});
        winner.put(1996, new String[]{"Carlos Filipe Ximenes Belo, José Ramos-Horta"});
        winner.put(1995, new String[]{"Joseph Rotblat, Pugwash Conferences on Science and World Affairs"});
        winner.put(1994, new String[]{"Yasser Arafat, Shimon Peres, Yitzhak Rabin"});
        winner.put(1993, new String[]{"Nelson Mandela", "Frederik Willem de Klerk"});

    }
    
    /**
     * @return the number of winners of given year. 
     */
    public int getNumberOfWinners(){
        int r = 0;
        Collection<String[]> s = winner.values();
        Iterator<String[]> it = s.iterator();
        while(it.hasNext()){
            String[] w = it.next();
            r += w.length; // r = r + w.length;
        }
        return r;
    }
    
    public int getNumberOfWinnersForYear(int year){
        
    }
}
Link to comment
https://linustechtips.com/topic/616584-java-stuck-again/
Share on other sites

Link to post
Share on other sites

6 minutes ago, elpiop said:

don't you just need to return


winner.get(year).length ?

that will tell you the length of the array (number of winners i guess?) for whatever year value you input

 

It...was that simple?!? What... @#$*&%^!@%$ thank you... 

I thought I had to make a loop and check the number of winners... so I kept making Iterator & Collection ones like I did in the previous method. Then it didn't work and I tried making a while loop as well as a for-each loop. 

Link to comment
https://linustechtips.com/topic/616584-java-stuck-again/#findComment-7962075
Share on other sites

Link to post
Share on other sites

26 minutes ago, tomoki said:

 

It...was that simple?!? What... @#$*&%^!@%$ thank you... 

I thought I had to make a loop and check the number of winners... so I kept making Iterator & Collection ones like I did in the previous method. Then it didn't work and I tried making a while loop as well as a for-each loop. 

well you could do something like this using an enhanced for loop

 

public int getNumberOfWinnersForYear(int year){
    	for ( Map.Entry<Integer, String[]> entry : winner.entrySet()) {
    	    Integer key = entry.getKey();
    	    String[] winners = entry.getValue();
    	    if(year == key)
    	    {
    	    	return winners.length; //return length of array (number of winners for that year)
    	    }
   	    
    	}
		return 0; //no winners
    }

however, since the get() method can be called on a hashmap, you can retrieve the entry, which in this case is your string array of winners, and then get its length easily, so no loop like this is necessary.

Link to comment
https://linustechtips.com/topic/616584-java-stuck-again/#findComment-7962132
Share on other sites

Link to post
Share on other sites

I can never figure out why Java considers the "dictionary" type obsolete. It is so much easier because you could just do this:

public int GetWinners(int year)
{
	return myDictionary[year].Length;
}

Alternatively, what @elpiop said.

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/616584-java-stuck-again/#findComment-7965516
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×