arraylist - Java issue, Can't print both methods -
bundle messagelist; import java.util.arraylist; import java.util.list; import java.util.scanner; import java.util.random; public class messagelist { public static void main(string[] args) { //create arraylist object list list = new arraylist(); //add elements arraylist list.add(0, "i love java programming."); list.add(1, "java programming much fun."); list.add(2, "who needs c ++, when have java."); list.add(3, "java can hard."); list.add(4, "i happy when a."); list.add(5, "i love jave coffee also."); list.add(6, "who wants larn java."); list.add(7, "i'm taking holiday after class."); list.add(8, "only 2 more questions go."); list.add(9, "what course of study should take next?"); system.out.println("retrieving stored messages arraylist"); shoutoutcannedmessage(list); } //this method retrieves values arraylist using method public static void shoutoutcannedmessage(list list) { //retrieving values list int size = list.size(); for(int i=0;i<size;i++) { system.out.println(list.get(i)); system.out.println("**********************\n\n"); } int userindex; scanner scanner = new scanner(system.in); system.out.println("please come in index number wish search"); userindex = scanner.nextint(); system.out.println(list.get(userindex)); }
the above method prints expected below code not. project i'm required have shoutoutcannedmethod
, shoutoutrandommethod
, canned method works.
public static void shoutoutrandommessage() { int i; //holds words generated. string[] subject= {" he's", " i'm", " she"}; string[] verb= {" eating", " catching", " studying", " caughing"}; string[] adjective= {" funny", " hard", " good", " polite"}; string[] object= {" course", " homework", " books", " dog"}; string[] adverb= {" quickly. ", " everywhere. ", " accordingly. ", " awfully. "}; random r = new random(); //intialize random int selectedelement = r.nextint(subject.length); //randomly create sentence. (i=1; i<=1; i++) { string randomsentence=subject[selectedelement] + verb[selectedelement] + adjective[selectedelement] + object[selectedelement] + adverb[selectedelement]; system.out.println("shoutout: " + randomsentence ); } }
you don't phone call method shoutoutrandommessage never executed.
add
shoutoutrandommessage();
in main method below
shoutoutcannedmessage(list);
and print out random message after come in index want first method.
doing got result:
please come in index number wish search 3 java can hard. shoutout: i'm catching hard homework everywhere.
java arraylist
No comments:
Post a Comment