java - Retrieving a random item from ArrayList -


i'm learning java , i'm having problem arraylist , randomgenerator.

i have object called catalogue has array list of objects created class called item. need method in catalogue returns information on 1 of itemobjects in list. item needs selected @ random. have used random generator util cannot working. can't work out have done wrong.

import java.util.arraylist; import java.util.random;  public class catalogue {     private random randomgenerator;     private arraylist<item> catalogue;      public catalogue ()     {         catalogue = new arraylist<item>();       }      public item anyitem()     {         int index = randomgenerator.nextint(catalogue.size());         return catalogue.get(index);         system.out.println("managers choice week" + anyitem + "our recommendation you");     } 

when try compile error pointing @ system.out.println line saying 'cannot find symbol variable anyitem'

any appreciated :) thanks

anyitem method , system.out.println call after return statement won't compile anyway since unreachable.

might want re-write like:

import java.util.arraylist; import java.util.random;  public class catalogue {     private random randomgenerator;     private arraylist<item> catalogue;      public catalogue()     {          catalogue = new arraylist<item>();         randomgenerator = new random();     }      public item anyitem()     {         int index = randomgenerator.nextint(catalogue.size());         item item = catalogue.get(index);         system.out.println("managers choice week" + item + "our recommendation you");         return item;     } } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -