What's new

Closed Login using array

Status
Not open for further replies.

DubuTofu

Eternal Poster
Joined
Aug 21, 2016
Posts
457
Reaction
1,319
Points
315
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

Back
Top