dimanche 7 juin 2020

Storing data to Java Array without using .TXT File

I'm seeking on how to store user inputs in Java Array without .TXT File integrated. After that, I need also to output how many users did input data in that certain Main Menu. I need help please. Thank you!

Data to input: Name and age

Example of outputting data count:

Current number of recruits:

Basketball team = 1 John

Volleyball team = 2

Arya

Dany

package javaapplication2;
import java.util.Scanner;

public class JavaApplication2 {

    public static void main(String[] args) {
        int choice, age;
        String name;

        String basketballName[2] = {};
        int basketballIndex = 0;
        String volleyballName[3] = {};
        int volleyballIndex = 0;


        // TODO code application logic here
        System.out.print("Select team");
        System.out.print("\n");
        System.out.print("--------------");
        System.out.print("\n");
        System.out.print("[1] Basketball");
        System.out.print("\n");
        System.out.print("[2] Volleyball");
        System.out.print("\n");
        System.out.print("[3] Open Slots Left");
        System.out.print("\n");
        System.out.print("[4] Exit");
        System.out.print("\n");
        System.out.print("--------------");
        System.out.print("\n");

        Scanner scan=new Scanner(System.in);
        System.out.print("Choice: ");
        choice=scan.nextInt();

      switch(choice)
        {
            case 1:
                System.out.print("Enter name: ");
                name=scan.nextLine();
                System.out.print("Enter age: ");
                age = scan.nextInt();
                //applying condition on qualified age
                if (age >= 18 && age <= 21){
                **Array[Index] = "name";**
                System.out.println("Welcome to the Basketball Team!");  
                }   
                else{  
                System.out.println("Sorry, you are not qualified!");
                }
            break;
            case 2:
                System.out.print("Enter name: ");
                name=scan.nextLine();
                System.out.print("Enter age: ");
                age = scan.nextInt();

                //applying condition on qualified age
                if(age >= 17 && age <= 19){    
                System.out.println("Welcome to the Volleyball Team!");  
                }   
                else{  
                System.out.println("Sorry, you are not qualified!");
                }
            break;
        }

    }
}

Here are the errors that I encountered:

Exception in thread "main" java.util.InputMismatchException

Cannot find symbol

Aucun commentaire:

Enregistrer un commentaire