1

以下 java 代码在 DR.java IDE 的控制台窗口中执行。我有以下两个问题请朋友们帮帮我。

  1. 是否可以进行密码屏蔽?我通过谷歌搜索尝试了很多,但没有一个对我有用(应该只使用控制台窗口进行执行)。
  2. 当我调用“GetLoginDetails();” 在“ShowAdminMainMenuFun(String EmpName)”方法中显示错误([行:148] 错误:未处理的异常类型 java.io.IOException)。我想过制作递归函数,但它确实有效,你可以纠正编码并将其发回。

谢谢你的朋友

{
import java.io.*;
import java.awt.*;
import java.io.Console;
import java.util.Scanner;

/*
UserLoginAuthentiction UserLoginAuthentictionObj = new UserLoginAuthentiction();
UserLoginAuthentictionObj.GetLoginDetails();
*/

class UserLoginAuthentiction
{
  static String EmployeeID,Password;
  public static byte resultRole = 0;
  public static void main(String[] args) throws Exception 
  {
    GetLoginDetails();   // it works well here but not works when called againin the following code
  }
  static void GetLoginDetails() throws IOException
  {
    Scanner sc = new Scanner(System.in);
    byte resultRole = 0;
    byte CountForLogin = 0;
    System.out.println("Totally 3 attempts ");  

    do
    {

      if(CountForLogin<3){
      System.out.print("\nEnter User Name:");
      EmployeeID = sc.nextLine();

      System.out.print("Enter Password :");



      Password = sc.nextLine();



      resultRole = ValidateUserIDAndPassword(EmployeeID,Password);
      // if result is zero then the login is invalid,
      // for admin it is one , 
      // for quality analyser it is 2 
      // for project developer it is 3 
      // for developer it is 4
      if(resultRole==0)
      {
        System.out.println("Username & Password does not match ");    
        System.out.print("Retry ::");
        CountForLogin++;
        if(CountForLogin>2)
        {System.out.println("ur attempts are over is locked");}

      }
      else
      {
        System.out.println("here t should call the appropriate employe function");

        GetRoleAndAssignFun(EmployeeID,resultRole);
        break;
      }
      }
    }while(resultRole==0);
  }
  static byte ValidateUserIDAndPassword(String EmployeeID,String Password)
  {

    byte resultRole = 0;

    if((EmployeeID.equals("tcs"))&&(Password.equals("tcs")))
    {
       resultRole = 1;

    }

    /*

     Code for checking the arraylist and returning the validations
     this method should return the roles of the users password

     */

    return resultRole;
  }
  static void GetRoleAndAssignFun(String EmpName ,int EmpRole)
  {   
    // System.out.println("  ");  
    switch(EmpRole)
    {
      case 1:
        System.out.println(" hi " +EmpName+ " u are logged in as admin ");
        ShowAdminMainMenuFun(EmpName);
        break;
      case 2:
        System.out.println(" hi " +EmpName+ " u are logged in as QA ");
        //  QualityAnalyserMainMenu(EmpName);
        break;
      case 3:
        System.out.println(" hi " +EmpName+ " u are logged in as PM ");
        //  ProjectMAnagerMainMenu(EmpName);
        break;
      case 4:
        System.out.println(" hi " +EmpName+ " u are logged in as DEVeloper ");
        //  DeveloperMainMenu(EmpName);
        break;
      default:
        //  System.out.println(EmpName +" You dont have any roles asigned ");
        break;
    }       
  } 


  public static void ShowAdminMainMenuFun(String EmpName)
  {   
    Scanner sc = new Scanner(System.in);
    int loop_option=0;
    do
    {

      //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

      System.out.println(" Hi "+ EmpName + " you can perform task ussing the menu given below");

      System.out.println("press the appropriate option only");
      System.out.println("1.Create New Employe Profile ");
      System.out.println("2.See Employee's Profile ");
      System.out.println("3. LogOut ");

      System.out.println("Enter the Option u need:");               
      int  option = sc.nextInt();
      switch(option)
      {
        case 1:
          System.out.println("1.Creating New Employe Profile");
          //CreateNewEmployeeProfile();
          break;
        case 2:
          System.out.println("2.See Employee's Profile ");
          //  ViewEmployeeProfile();
          break;
        case 3:
          System.out.println("3. LogOut");
          System.out.println("Do u want to continue logging out  ?? If yes Press 1  ..");
          boolean ConformLogout = false;

          ConformLogout = sc.nextBoolean();
          if(ConformLogout)
          {
            **GetLoginDetails();**   //**** error is here */ how can i call this function please help me
          }
          else
          {

          }
          //   LogOut();
          break;        
        default :
          System.out.println(" You .. ");
      }
      System.out.println("Do u want to continue to main menu ?? Press 1 to continue..");
      loop_option = sc.nextInt();
    }while(loop_option==1);     
  }  


}
}
4

1 回答 1

1

Regarding your first question,

Is it possible to make password masking?

You can use java.io.Console class to hide the password on console window.

于 2011-02-09T05:01:04.800 回答