1

I would like to have the inputs of options 1 and 2 be printed in option 3. I have a menu setup and the user chooses which number they want and then there are follow up questions after which team they choose.

    if (choys == 1)
    {
        ch = new Scanner(System.in);
        System.out.println("Enter name: ");
        userBName = ch.nextLine();

        System.out.println("Enter age: ");
        userBAge = ch.nextInt();

        if(userBAge >=18 && userBAge <=21)
        {
            System.out.println("Congrats "+userBName+"! Welcome to the team.");
            bslot++;
        }
        else
        {
            System.out.println("Sorry "+userBName+". You are not qualified.");
        }

    }

    if (choys == 2)
    {
        System.out.println("Enter name: ");
        userVName = ch.nextLine();

        System.out.println("Enter age: ");
        userVAge = ch.nextInt();

        if(userVAge >=18 && userVAge <=21)
        {
            System.out.println("Congrats "+userVName+"! Welcome to the team.");
            vslot++;
        }
        else
        {
            System.out.println("Sorry "+userVName+". You are not qualified.");
        }
    }

    if (choys == 3)
    {
        System.out.println("Current number of recruits:\n");
        System.out.println("Basketball team: "+ userBName+"\n\n");
        System.out.println("Volleyball team: "+ userVName);
    }
    }
Cardinal
  • 23
  • 4
  • Please be precise about your question and post enough details about it – Adam Jun 01 '19 at 16:25
  • Please read [mcve] and enhance your question accordingly. Also : please format / indent all of your code, instead dumping such a messy piece of work on us. – GhostCat Jun 01 '19 at 16:31

2 Answers2

1

We can use 2 different ArrayList to store the usernames in Option 1 and 2. Then print the same ArrayList in Option 3.

List<String> vNames = new ArrayList();
List<String> bNames = new ArrayList();

if (choys == 1)
{
    ch = new Scanner(System.in);
    System.out.println("Enter name: ");
    userBName = ch.nextLine();

    System.out.println("Enter age: ");
    userBAge = ch.nextInt();

    if(userBAge >=18 && userBAge <=21)
    {
        System.out.println("Congrats "+userBName+"! Welcome to the team.");
        bslot++;
        bNames.add(userBName);
    }
 }

if (choys == 2)
{
    ch = new Scanner(System.in);
    System.out.println("Enter name: ");
    userVName = ch.nextLine();

    System.out.println("Enter age: ");
    userVAge = ch.nextInt();

    if(userVAge >=18 && userVAge <=21)
    {
        System.out.println("Congrats "+userVName+"! Welcome to the team.");
        vSlot++;
        vNames.add(userVName);
    }
 }
 if (choys == 3)
{
    System.out.println("Current number of recruits:\n");
    System.out.println("Basketball team: ");
    for(String name:bNames){
        System.out.println("Username : "+ name);
    }

}
Shashank Gupta
  • 159
  • 2
  • 10
0

I had tried to extend @Shashank's code by using while loop to let the vNames and bNames stay in ArrayList.

        Scanner ch = new Scanner(System.in);
        List<String> vNames = new ArrayList<String>();
        List<String> bNames = new ArrayList<String>();

        int choys;
        int bslot = 0, vslot = 0;
        Boolean run = true;
        while (run) {
            System.out.println("Enter Choice");
            choys = Integer.parseInt(ch.next());
            if (choys == 1) {
                System.out.println("Enter name: ");
                String userBName = ch.next();

                System.out.println("Enter age: ");
                int userBAge = Integer.parseInt(ch.next());

                if (userBAge >= 18 && userBAge <= 21) {
                    System.out.println("Congrats " + userBName
                            + "! Welcome to the team.");
                    bslot++;
                    bNames.add(userBName);
                }
            }else if (choys == 2) {
                ch = new Scanner(System.in);
                System.out.println("Enter name: ");
                String userVName = ch.next();

                System.out.println("Enter age: ");
                int userVAge = Integer.parseInt(ch.next());

                if (userVAge >= 18 && userVAge <= 21) {
                    System.out.println("Congrats " + userVName
                            + "! Welcome to the team.");
                    vslot++;
                    vNames.add(userVName);
                }
            } else if (choys == 3) {
                System.out.println("Current number of recruits:\n");
                System.out.println("Basketball team: ");
                for (String name : bNames) {
                    System.out.println("Username : " + name);
                }
                System.out.println("Volleyball team: ");
                for (String name : vNames) {
                    System.out.println("Username : " + name);
                }

            } else {
                run = false;
            }
        }

        System.out.println("Program Ended");
Nitika
  • 555
  • 3
  • 8