java - How to write a simple input validation for this code -
i need on writing user input validation programm. have been given homework write java program, using array or arraylist, store 10 messages in , ask user choose between 0 , 10, display message , generate random message.
i able complete program , submit it, program crashes if user puts other character integer, or if puts integer,v not within range of 0 10.
i want resubmit homework input validation better grade decided ask pros, once again started learning programming 5 weeks ago.
below homework submitted:
public static void main(string[] args) { system.out.println("please read through folowing 10 messages , follow instruction under them."); list<string> list = new arraylist <string>(10); // making object in arraylist //create arraylist object //add elements arraylist list.add(0, "i try good."); list.add(1, "nobody perfect."); list.add(2, "life good"); list.add(3, "this me."); list.add(4, "system out"); list.add(5, "it's summer time"); list.add(6, "i green pepper,"); list.add(7, "he funny"); list.add(8, "there challenges"); list.add(9, "what name"); shoutoutcannedmessage(list); system.out.println("retrieving stored messages arraylist"); shoutoutrandommessage(); } //this method retrieves values arraylist using method public static void shoutoutcannedmessage(list<string> message) { int size = message.size(); for(int i=0;i<size;i++) { system.out.println(message.get(i)); } // retrieve user's choice int userchoice; scanner scanner = new scanner(system.in); // inform user select 1 of messages poped above system.out.println("please enter number of choice 0 10"); userchoice = scanner.nextint(); system.out.println(message.get(userchoice)); } /** * */ public static void shoutoutrandommessage() { //holds words generated. string[] subject= {" he", " me", " she", "we"}; string[] verb= {" do", " say", " get", " make", " know"}; string[] adjective= {" good", " new", " first", " last", " long"}; string[] object= {" cup", " map", " house", " computer"}; string[] adverb= {" up. ", " so. ", " out. ", " now. ", " just"}; random r = new random(); //intialize random int selectedelement = r.nextint(subject.length); //randomly create sentence. { string randomsentence=subject[selectedelement] + verb[selectedelement] + adjective[selectedelement] + object[selectedelement] + adverb[selectedelement]; system.out.println("shoutout: " + randomsentence ); } } }
the number user stored in variable userchoice
. need check if number valid. can adding if
.
replace code:
system.out.println("please enter number of choice 0 10"); userchoice = scanner.nextint(); system.out.println(message.get(userchoice));
with this:
system.out.println("please enter number of choice 0 10"); userchoice = scanner.nextint(); if(userchoice >=0 && userchoice <=10) system.out.println(message.get(userchoice)); else system.out.println("the number entered not valid.");
you can go step further , check if user entered valid number. can done by:
system.out.println("please enter number of choice 0 10"); string userchoicestr = scanner.next(); try{ userchoice = integer.parseint(userchoicestr); if(userchoice >=0 && userchoice <=10) system.out.println(message.get(userchoice)); else system.out.println("the number entered not valid."); } catch (numberformatexception e) { system.out.println("the number entered not valid."); }
to let user try again if entered wrong input: want process repeat if enters wrong input. repeating processes means loops. there many constructs in java write loops. in case best choice use do-while
loop want user try @ least once.
boolean isinputvalid = false; do{ system.out.println("please enter number of choice 0 10"); string userchoicestr = scanner.next(); try{ userchoice = integer.parseint(userchoicestr); if(userchoice >=0 && userchoice <=10) { system.out.println(message.get(userchoice)); isinputvalid = true; } else system.out.println("the number entered not valid."); } catch (numberformatexception e) { system.out.println("the number entered not valid."); } } while (!isinputvalid);
Comments
Post a Comment