-2

So i'd been asked to make a program which have a login page And i should use vector to make this login page. (Java Language) if i only register 1 user on this program. it will work, but if i resist more than 1 user, it will automatically break and said indexoutofbound. This is the code

import java.util.Scanner;
import java.util.Vector;
public class T028_J1H2 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input= new Scanner (System.in);

    int choice;
    String user;
    String pass;
    String age;
    String address;
    String email;
    String hobby;
    int count=0;
    Vector<String> username = new Vector<String>();
    Vector<String> password = new Vector<String>();
    Vector<String>umur= new Vector<String>();
    Vector<String>alamat= new Vector<String>();
    Vector<String>temp_email = new Vector<String>();
    Vector<String>hobi = new Vector<String>();
    Vector<Vector<String>> profile = new Vector<Vector<String>>();
    do{
    System.out.println("1. Login");
    System.out.println("2. Register");
    System.out.println("3. Exit");
    System.out.print("Choose : ");
    choice = input.nextInt();
    input.nextLine();
    switch (choice)
    {
        case 1:
            int pilihan;
            int index=0;
            String login_user;
            String login_password;
            if(profile.isEmpty())
            {
                System.out.println("Please Register First");

            }
            else
            {
                int flag_user=0;
                int flag_pass=0;
                do{
                System.out.print("User : ");
                login_user=input.nextLine();
                System.out.print("Password : ");
                login_password=input.nextLine();
                for(int i=0;i<count;i++)
                {
                    if(login_user.equals(username.get(i)))
                            {

                                flag_user=1;
                            }
                    if(login_password.equals(password.get(i)))
                            {
                                flag_pass=1;
                            }
                }
                if(flag_user==0 || flag_pass==0)
                {
                    System.out.println("Username/Password Wrong");
                }
                else if(flag_user==1 && flag_pass==1)
                {
                    index++;
                    break;
                }
                }while(flag_user==0 || flag_pass==0);
                if(flag_user==1 && flag_pass==1)
                {
                    do{
                System.out.println("1. View Profile");
                System.out.println("2. Buy Product");
                System.out.println("3. View Product");
                System.out.println("4. Insert Product");
                System.out.println("5. Update Product");
                System.out.println("6. Delete Product");
                System.out.println("7. Exit");
                System.out.print("Choose : ");
                pilihan=input.nextInt();input.nextLine();
                switch (pilihan)
                {
                case 1:
                    System.out.println("View Profile");
                    System.out.println("=========================");
                    System.out.println("User : " + profile.get(index-1).get(0));
                    System.out.println("Age : "+profile.get(index-1).get(2));
                    System.out.println("Address : "+profile.get(index-1).get(3));
                    System.out.println("Email : "+profile.get(index-1).get(4));
                    System.out.println("Hobby : "+profile.get(index-1).get(5));
                    break;
                case 2:

                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                }
                    }while(pilihan!=7);
                }
            }
        break;

        case 2:
            username = new Vector<String>();
            password = new Vector<String>();
            umur= new Vector<String>();
            alamat= new Vector<String>();
            temp_email = new Vector<String>();
            hobi = new Vector<String>();
        System.out.println("Please fill the data first");
        System.out.println("=======================================");
        System.out.print("Input your username[5..20 | must be Alphanumeric] : ");
        user=input.nextLine();
        username.add(user);
        System.out.print("Input Your Password[5..20 | must be Alphanumeric] : ");
        pass=input.nextLine();
        password.add(pass);
        System.out.print("Input your age[17-50] : ");
        age=input.nextLine();
        umur.add(age);
        System.out.print("Input your address : ");
        address=input.nextLine();
        alamat.add(address);
        System.out.print("Input your e-mail : ");
        email=input.nextLine();
        temp_email.add(email);
        System.out.print("Input your hobby : ");
        hobby=input.nextLine();
        hobi.add(hobby);
        profile.add(username);
        profile.add(password);
        profile.add(umur);
        profile.add(alamat);
        profile.add(temp_email);
        profile.add(hobi);
        System.out.println("Register Successful!");
        input.nextLine();
        count++;

        break;
        case 3:

        break;
    }
    }while(choice!=3);



}

}
Jasson Harsojo
  • 237
  • 1
  • 6
  • 15

1 Answers1

1

You are re-initializing username each time you register. The count variable continues to increase, but you will only ever have one username in your Vector.

JeredM
  • 897
  • 1
  • 14
  • 25