🔒 Closed Login using array

Status
Not open for further replies.

DubuTofu

Honorary Poster
Good day mga kaphc, ask ko lang kung bat may error pagwala sa array yung iniinput ko? Pasensya po magulo code ko

Code:
package com.bankingsys;



import com.bankingsys.BankingSystem;
import java.util.Scanner;
public class BankingSystem{

    public static String username[]= {"hakdog","lloyd","dekudeku"};
    
      public static String password[]={"lloydkun","samson","deku"};
      
      public static String userOne="",userOneP="",changeP="";
      
      public static int storeLog=0;
      public static int length=username.length;
      
      
  public static void main(String[] args) {
   Login();
  }
  static void Login(){
    Scanner sc = new Scanner(System.in);
      
      
       System.out.println("LOGIN");
       System.out.print("Enter username:");
       userOne = sc.next();
        System.out.print("Enter password:");
        userOneP = sc.next();
 
              
        for(int i = 0; i<=length; i++){
            if(userOne.equals(username[i])){
                storeLog = i;
                break;
            }
          
        }
        Checker();
      
  }
 
  static void Checker(){
    if(userOne.equals(username[storeLog]) && userOneP.equals(password[storeLog])){
          System.out.println("Success");
          ChangePw();
      }else{
        System.out.println("Wrong!");
        Login();
      }
  }
 
  static void  ChangePw(){
  Scanner sc = new Scanner(System.in);
    System.out.print("Enter new Pin: ");
    password[storeLog]=sc.next();
    Login();
  }
 
}
 
Code:
package com.bankingsys;



import com.bankingsys.BankingSystem;
import java.util.Scanner;
public class BankingSystem{

    public static String username[]= {"hakdog","lloyd","dekudeku"};
    
      public static String password[]={"lloydkun","samson","deku"};
      
      public static String userOne="",userOneP="",changeP="";
      
      public static int storeLog=0;
      public static int length=username.length;
      
      
  public static void main(String[] args) {
   Login();
  }
  static void Login(){
    Scanner sc = new Scanner(System.in);
      
      
       System.out.println("LOGIN");
       System.out.print("Enter username:");
       userOne = sc.next();
        System.out.print("Enter password:");
        userOneP = sc.next();
 
// Check Username 4 times but the size of the array username is 3? magththrow po ito ng 
// arrayindexoutofbounds kasi walang index 3 sa username. Index 0 to 2 lang
// Solution: Change the "i<=length" to i<length 
        for(int i = 0; i<=length; i++){
            if(userOne.equals(username[i])){
                storeLog = i;
                break;
            }
          
        }
        Checker();
      
  }
 
  static void Checker(){
    if(userOne.equals(username[storeLog]) && userOneP.equals(password[storeLog])){
          System.out.println("Success");
          ChangePw();
      }else{
        System.out.println("Wrong!");
        Login();
      }
  }
 
  static void  ChangePw(){
  Scanner sc = new Scanner(System.in);
    System.out.print("Enter new Pin: ");
    password[storeLog]=sc.next();
    Login();
  }
 
}


Check Username 4 times but the size of the array username is 3? magththrow po ito ng
arrayindexoutofbounds kasi walang index 3 sa username. Index 0 to 2 lang
Solution: Change the "i<=length" to i<length

Code:
        for(int i = 0; i<length; i++){
            if(userOne.equals(username[i])){
                storeLog = i;
                break;
            }
          
        }
 
Code:
package com.bankingsys;



import com.bankingsys.BankingSystem;
import java.util.Scanner;
public class BankingSystem{

    public static String username[]= {"hakdog","lloyd","dekudeku"};
   
      public static String password[]={"lloydkun","samson","deku"};
     
      public static String userOne="",userOneP="",changeP="";
     
      public static int storeLog=0;
      public static int length=username.length;
     
     
  public static void main(String[] args) {
   Login();
  }
  static void Login(){
    Scanner sc = new Scanner(System.in);
     
     
       System.out.println("LOGIN");
       System.out.print("Enter username:");
       userOne = sc.next();
        System.out.print("Enter password:");
        userOneP = sc.next();
 
// Check Username 4 times but the size of the array username is 3? magththrow po ito ng
// arrayindexoutofbounds kasi walang index 3 sa username. Index 0 to 2 lang
// Solution: Change the "i<=length" to i<length
        for(int i = 0; i<=length; i++){
            if(userOne.equals(username[i])){
                storeLog = i;
                break;
            }
         
        }
        Checker();
     
  }
 
  static void Checker(){
    if(userOne.equals(username[storeLog]) && userOneP.equals(password[storeLog])){
          System.out.println("Success");
          ChangePw();
      }else{
        System.out.println("Wrong!");
        Login();
      }
  }
 
  static void  ChangePw(){
  Scanner sc = new Scanner(System.in);
    System.out.print("Enter new Pin: ");
    password[storeLog]=sc.next();
    Login();
  }
 
}


Check Username 4 times but the size of the array username is 3? magththrow po ito ng
arrayindexoutofbounds kasi walang index 3 sa username. Index 0 to 2 lang
Solution: Change the "i<=length" to i<length

Code:
        for(int i = 0; i<length; i++){
            if(userOne.equals(username[i])){
                storeLog = i;
                break;
            }
         
        }
Kuya RollerCoaster pano gawin * asterisk yung password sa java?
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 11
    Replies
  • 1K
    Views
  • 8
    Participants
Last reply from:
Arcturus

Trending Topics

Online now

Members online
1,101
Guests online
1,022
Total visitors
2,123

Forum statistics

Threads
2,277,954
Posts
28,980,038
Members
1,228,184
Latest member
Shinra09
Back
Top