java - Unable to access ArrayList object method -


i'm trying write program randomly picks 3 countries pool, outputs them string. these countries receive gold, silver, , bronze in order. method olympicresults add each string array. eventually, countries sorted number of gold, silver , bronze medals attained.

i'm having trouble eventresult method - can't seem access methods country class, getname() - gives me cannot find symbol error. seemed work before when initialized arraylist in eventresult method, wouldn't work since new arraylist initialized every time method called.

another problem preventing repeats of countries appearing in eventresult string - ideas of did wrong?

code:

public class olympicmedals  { public static void main (string[] args) {     string[] countries = {"can", "brb", "blr", "hkg", "chn", "swe"}     arraylist<country> list = new arraylist<country>();     (int j=0; j<9; j++) //initialize countries     {         list.add(new country());         list.get(j).setname(countries[j]);     }      scanner scan = new scanner(system.in);     int n = scan.nextint();      print(n, list);  }   public static string eventresult (arraylist list) //generates result of discipline {     string res = "";     random rand = new random();     arraylist<country> listcopy = new arraylist<country>(list);       (int i=0; i<3; i++) //generate list, prevent repeats     {           int n = listcopy.size();         int random = rand.nextint(n);         res += list.get(random).getname() + " ";          if (i==0)             list.get(random).setgold(1);         if (i==1)             list.get(random).setsilver(1);         if (i==2)             list.get(random).setbronze(1);          listcopy.remove(random);     }     return res;            }  public static string[] olympicresults(int n, arraylist list) //array of strings eventresult {     string[] result;     result = new string[n];      (int i=0; i<n; i++)     {         result[i] = eventresult(list);     }     return result; }  public static void print(int n, arraylist list) {     (int i=0; i<n; i++) //print results     {         system.out.println(olympicresults(n, list)[i]);     } } 

country class:

public class country  { private int gold; private int silver; private int bronze; private string name;  public country() //default constructor {     setname("");     setgold(0);     setsilver(0);     setbronze(0); }  public void setname(string n) {     name = n; }  public string getname() {     return name; }  public void setgold(int g) {     gold += g; }  public int getgold() {     return gold; }  public void setsilver(int s) {     silver += s; }  public int getsilver() {     return silver; }  public void setbronze(int b) {     bronze += b; }  public int getbronze() {     return bronze; } 

}

can try below code changes ?

public static string eventresult (arraylist<country> list) //generates result of discipline {     string res = "";     random rand = new random();     arraylist<country> listcopy = new arraylist<country>(list);       (int i=0; i<3; i++)  {          int random = rand.nextint(listcopy.size());         res += listcopy.get(random).getname() + " ";         if (i==0)             listcopy .get(random).setgold(1);         if (i==1)             listcopy .get(random).setsilver(1);         if (i==2)             listcopy .get(random).setbronze(1);          listcopy.remove(random); // remove country received medal list     }         return res;            } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -