2016-03-09 59 views
1
import java.util.*; 
import java.io.*; 


public class A3 { 

    public static void main(String args[]) 
    { 
     Accept inputScanner = new Accept(); 
     Assign3 sortObj = new Assign3(); 

     String lname[] = {"","","","",""}; 
     String psw[] = {"","","","",""}; 
     String input; 

      do 
      { 
       System.out.println("Password Generator");   
       System.out.println("Please enter 5 last names:"); 

       for (int index = 0; index < lname.length; index++) 
       { 
        System.out.print("Please enter last name: "); 
        lname[index] = inputScanner.acceptInputString(); 
        psw[index] = sortObj.generatePassword(lname, index);      
       } 

       sortObj.sortArrayDescending(lname);    
       sortObj.arrayDisplay(lname, psw); 

       Screen.scrollscreen(70, 1, '='); 
       System.out.print("Please enter name to search (e or E to exit):"); 
       input = inputScanner.acceptInputString(); 
       int password = sortObj.binSrch(lname, input); 

       if(password >= 0) 
       {     
        System.out.println("Name: " + input + " ===> " + "password: " + password); 
        System.out.println("++++++++++++++++++++++++++++++++++"); 
       } 
       else 
       { 
        System.out.println(input + " is not found"); 
        System.out.println("++++++++++++++++++++++++++++++++++"); 
       } 

      }while(input.equals("e") && input.equals("E")); 
    } 


    public void sortArrayDescending(String[] lnameArray) 
    { String temp; 

     for(int j = 1; j < lnameArray.length - 1; j++) 
     { 
      for(int index = 0; index < lnameArray.length - 1; index++) 
      { 
       if(lnameArray[index].trim().compareTo(lnameArray[index+1].trim())<0) 
       { 
        temp = lnameArray[index]; 
        lnameArray[index] = lnameArray[index + 1]; 
        lnameArray[index + 1] = temp; 
       } 
      }     
     } 
    } 

    public void arrayDisplay(String lnameContent[], String pswContent[]) 
    { 
     for(int i = 0; i < lnameContent.length; i ++) 
     { 
      System.out.println(lnameContent[i] + "  " + pswContent[i]); 
     } 
    } 

    public int binSrch(String strArr[], String search) 
    {   
      int mid = -1; 
      int first = 0; 
      int last = strArr.length - 1; 
      boolean found = false; 

      while(first <= last) 
      { 
       mid = (first + last)/2; 

       if(strArr[mid].equalsIgnoreCase(search)) 
       { 
        found = true; 
        break; 
       } 
       else if(strArr[mid].compareToIgnoreCase(search) < 0) 
       { 
        last = mid - 1; //use lower half 
       } 
       else 
       { 
        first = mid + 1; //use upper half 
       } 
      } 

      if(!found) 
      { 
       mid = -1; 
      } 

      return mid; 

    } 

    public String generatePassword(String[] str, int index)   
    { 
     String name = str[index]; 
     char first = name.charAt(0); 
     char last = name.charAt(name.length()-1); 
     int mid = 0; 

     if(first != last) 
     { 
      mid = (((int)(first) + ((int)(last))/2)); 
     } 
     else 
     { 
      mid = (((int)(first) + ((int)(last))/3)); 
     } 

     Random rNum = new Random(); 
     int randomNum = rNum.nextInt(5); 
     String password = (first + "" + (char)(mid) + "" + randomNum).toLowerCase(); 

     return password; 
    } 

    public void duplicateCheck(String[] password) 
    { 

    } 
} 

輸出爲什麼我的二分查找找不到我爲我的字符串數組生成的密碼?

Password Generator 
Please enter 5 last names: 
Please enter last name: magnum 
Please enter last name: bauer 
Please enter last name: sahid 
Please enter last name: austen 
Please enter last name: reese 
sahid  m?0 
reese  b?0 
magnum  s¥4 
bauer  a?0 
austen  r¤0 
====================================================================== 
Please enter name to search (e or E to exit):sahid 

Name: sahid ===> password: 0  

++++++++++++++++++++++++++++++++++ 

//this line is supposed to print out "m?0" because it is the password 

這是對 「薩希德」 那麼,爲什麼打印 「0」 產生的?我怎樣才能做到這一點

打印出「m?0」或任何我的程序生成的隨機密碼?

回答

0

您的代碼是正確的。請記住passwordpsw[]字符串數組的索引,而不是數組的實際內容。

你只需要編寫psw[password]而不是password以下行:

System.out.println("Name: " + input + " ===> " + "password: " + password); 
/* Correction Here : Replace password --> psw[password] */ 

更正代碼片段:

if(password >= 0) 
{     
    System.out.println("Name: " + input + " ===> " + "password: " + psw[password]); 
    System.out.println("++++++++++++++++++++++++++++++++++"); 
} 

現在,你應該得到的輸出如下:

Name: sahid ===> password: m?0 

編輯:此外,正如@Gyanapriya所提到的,除非您嘗試將隨機密碼分配給用戶,否則除lname[]之外,還必須對psw[]數組進行排序。

+0

應用您提出的更改後:你不認爲這仍然是錯誤的。 sortDescending函數需要交換lname和psw的元素以保持與輸入相同的順序。 – Gyan

+0

@Gyanapriya是的,除非他試圖將隨機密碼分配給用戶,否則你是正確的。 – user2004685

相關問題