0

I'm making a program where a user enters how many inputs the user wants. Example if the user enter's 3, then 3 user inputs will appear. The output of this program goes something like this:

    ----------------------------------
    Enter How Many Inputs: 3

    enter name:
    rendell //value to be outputted

    enter age:
    20 //value to be outputted

    enter gender:
    male //value to be outputted
    -----------------------------------     

Now, I want to store those entered values somewhere to be outputted separately after but I have no idea how to do it. I tried to use the ctr1 variable but it only outputs the value of the last user input.

Here is my code:

    Object stud1 [][] = {{1,2,3},{"enter name:","enter age:","enter gender:"}};
    String ctr1;

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Scanner scan = new Scanner(System.in);


    System.out.print("Enter How Many Inputs: ");
    int num1 = Integer.parseInt(in.readLine());

    for (int i = 0; i<num1;i++){
         System.out.println(pers[1][i]);
         ctr1 = in.readLine();
            }
5
  • Is user always going to enter name, age and gender? Commented Aug 19, 2014 at 7:58
  • what's pers 2d array content? Commented Aug 19, 2014 at 7:59
  • @Nullpointer Depending on how many inputs the user enter. In that output, I entered 3 that's why it prompted me to enter name,age and gender, but if the user enters only 1, then only the name will prompt. Commented Aug 19, 2014 at 8:01
  • What if user enters 7? Commented Aug 19, 2014 at 8:01
  • @Nullpointer ArrayIndexOutOfBoundsException Commented Aug 19, 2014 at 8:03

1 Answer 1

1

The problem is that you are only assigning all the input in one reference to the String thus you can print all the input the user just gave.

solution:

You can use an array of String to put all the values from the inputted data if the user

sample:

    String [] choices = { "enter name:","enter age:","enter gender:"};
    String ctr1[];

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter How Many Inputs: ");
    int num1 = Integer.parseInt(scan.nextLine());
    ctr1 = new String[num1];

    for (int i = 0; i < num1; i++) {
        System.out.println(choices[i]);
        ctr1[i] = scan.nextLine();
    }

    for(int i = 0; i < ctr1.length; i++)
    {
        if(i == 0)
            System.out.println("Name: "+ ctr1[i]);
        else if( i == 1)
            System.out.println("Age: "+ ctr1[i]);
        else if( i == 2)
            System.out.println("Gender: "+ ctr1[i]);
    }

result:

Enter How Many Inputs: 3
enter name:
Rod_algonquin
enter age:
12
enter gender:
male
Name: Rod_algonquin
Age: 12
Gender: male
Sign up to request clarification or add additional context in comments.

1 Comment

He doesn't need the BufferedReader as well as the Object[] array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.