Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to be able to choose an array index using input.

Object stud1 [][] = {
          {1,2,3},
          {"favorite food: ","pet name: ","bday: "}
          }

System.out.println("how many inputs?");

If a user inputs 1, then "favorite food:" will prompt the user and if the user inputs 2, then both "favorite food: " and "pet name: " will prompt the user and so on.

After the user completes the prompt input, this will display:

  favorite food: chicken
  pet: doge
  birthday: december 25,1994

/////////////////////////////////////////my code/////////////////////////////////////////////

This question is similar to my other question, I just could not find the right answer for my question because I think it was confusing and not specific enough.

It's kind of working already but the problem is that when I input 1 then it still outputs everything. I only want it to output everything if the user inputs 3 which is the number of indexes in my array.

I am not pretty good with arrays yet especially 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) {
        for (int x = 1; x<stud1.length;x++){
            for (int i = 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++;
            }

        }
share|improve this question
    
What is stud1? You never declared stud1 and you never modify stud1 in your double for loop. –  Rohan Aug 19 '14 at 4:06

1 Answer 1

up vote 0 down vote accepted

I'm sorry but I don't think this is a good example for multidimensional arrays... nothing is really being done with the first element here: {1,2,3}.

To answer the specific question of why all 3 elements are printing out when the user only inputs a "1", it is because that value is being read into the variable num1, but num1 is not used anywhere in the loop that prints the output. If you want the input to control how many values are printed, then num1 needs to be used in the for loop's test expression (the middle phrase in the parentheses). I think a good first step is to change your inner loop like so:

for (int i = 0; i<num1;i++){
     System.out.println(stud1[1][i]);
     ctr1 =in.readLine();
}

Also note that the i++ is moved inside the parentheses for the for loop. That's really where it belongs, if you're using for.

Hope this helps!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.