getting an index from array and output it in java -
i want able choose array index using input.
object stud1 [][] = { {1,2,3}, {"favorite food: ","pet name: ","bday: "} } system.out.println("how many inputs?"); if user inputs 1, "favorite food:" prompt user , if user inputs 2, both "favorite food: " , "pet name: " prompt user , on.
after user completes prompt input, display:
favorite food: chicken pet: doge birthday: december 25,1994 /////////////////////////////////////////my code/////////////////////////////////////////////
this question similar other question, not find right answer question because think confusing , not specific enough.
it's kind of working problem when input 1 still outputs everything. want output if user inputs 3 number of indexes in array.
i not pretty arrays yet multi array, i'm still experimenting.
string ctr1; bufferedreader in = new bufferedreader(new inputstreamreader(system.in)); system.out.print("enter how many inputs: "); int num1 = integer.parseint(in.readline()); if (num1 <= stud1.length) { (int x = 1; x<stud1.length;x++){ (int = 0; i<stud1[x].length;){ /*system.out.print("enter value #" + x++ +":"); ctr1 =integer.parseint(in.readline()); i++;*/ system.out.println(stud1[x][i]); ctr1 =in.readline(); i++; } }
i'm sorry don't think example multidimensional arrays... nothing being done first element here: {1,2,3}.
to answer specific question of why 3 elements printing out when user inputs "1", because value being read variable num1, num1 not used anywhere in loop prints output. if want input control how many values printed, num1 needs used in for loop's test expression (the middle phrase in parentheses). think first step change inner loop so:
for (int = 0; i<num1;i++){ system.out.println(stud1[1][i]); ctr1 =in.readline(); } also note i++ moved inside parentheses for loop. that's belongs, if you're using for.
hope helps!
Comments
Post a Comment