vendredi 6 novembre 2020

How do I make the randomized passwords print on the .txt file and relay it back to me?

I was trying to make a randomized password generator for one of my assignments, and I need it to print onto a .txt file and then retrieve it through the text file when the user wanted to stop. The problem is that when I run it, it doesn't even put it on a .txt file, and therefore, it cannot present the user with the desired passwords! How do I make it so that it works using the code below?

import java.util.Scanner;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

public class SecretPassword

{

public static void main (String [ ] args) throws IOException

{

 String passwords = "";
  Scanner in = new Scanner(System.in);
  File fileName = new File("passwordsSheetDeleteWhenDone.txt");
  PrintWriter outFile = new PrintWriter(new File("passwordsSheetDeleteWhenDone.txt"));
  Scanner inFile = new Scanner (fileName);
  
  System.out.println("              Password Generator Menu               ");
  System.out.println("====================================================");
  System.out.println("| [1] Lowercase Letters                            |");
  System.out.println("| [2] Lowercase and Numbers                        |");
  System.out.println("| [3] Lowercase, Uppercase, & Numbers              |");
  System.out.println("| [4] Lowercase, Uppercase, Numbers, & Punctuation |"); 
  System.out.println("| [5] Quit                                         |");      
  System.out.println("====================================================");      
  System.out.print("Enter Selection (1-5): "); 
  int choice = in.nextInt();          
  
  while (choice != 5)
  {
     if (choice < 1 || choice > 4) 
     {
        System.out.println("Invalid option. Please try again.");
     }
     else
     {
        Scanner input = new Scanner(System.in);
        System.out.print("Password Length (6 or more): ");
        int lengthPass = input.nextInt(); 
        String password = "";
        char randomChar;
        double randNum = 0;
        int randNumber = 0;     
        if(lengthPass < 6) 
        {
           System.out.println("Password length too short. Please try again");
           Scanner input1 = new Scanner(System.in);
        }   
        else 
        { 
           if (choice == 1) 
           {           
              for(password = ""; password.length() < lengthPass; password += randomChar)
              {
                 randNum = (int)(Math.random() * 26) + 97;                
                 randomChar = (char)randNum; 
              }           
           }        
                 
           else if (choice == 2) 
           {
              for(password = ""; password.length() < lengthPass; password += randomChar) 
              {           
                 randNum = Math.random();
                 if (randNum < 0.5) 
                 {
                    randNum = (int)((randNum * 2) * 26) + 97;
                    randomChar = (char)randNum;
                 }
                 else 
                 {
                    randNum = (int)((randNum - 0.5) * 2 * 10) + 48;
                    randomChar = (char)randNum;
                 }                        
              }
           }
           else if (choice == 3) 
           {
              for(password = ""; password.length() < lengthPass; password += randomChar) 
              {                
                 randNum = Math.random();
                 if (randNum < 0.33) 
                 {
                    randNum = ((randNum * 3) * 26) + 97;              
                    randomChar = (char)randNum; 
                 }
                 else if (randNum < 0.66) 
                 {
                    randNum = (int)((randNum - 0.33) * 3 * 10) + 48;
                    randomChar = (char)randNum;
                 }
                 else {
                    randNum = (int)((randNum - 0.66) * 26) + 65;
                    randomChar = (char)randNum;
                 }        
              }        
           }         
           else if (choice == 4)
           {            
              for(password = ""; password.length() < lengthPass; password += randomChar) 
              {
                 randNum = Math.random();
                 if(randNum < 0.25) 
                 {
                    randNum = (int)((randNum * 4) * 26) + 97;
                    randomChar = (char)randNum;
                 }
                 else if(randNum < 0.5) 
                 {   
                    randNum = (int)((randNum - 0.25) * 4 * 10) + 48;
                    randomChar = (char)randNum;
                 }
                 else if(randNum < 0.75)
                 {
                    randNum = (int)((randNum - 0.5) * 4 * 26) + 65;
                    randomChar = (char)randNum;
                 }
                 else if(randNum < 0.87)
                 {
                    randNum = (int)((randNum - 0.75) * 4 * 4) + 91;
                    randomChar = (char)randNum; 
                 }
                 else 
                 {
                    randNum = (int)((randNum - 0.87) * 4 * 3) + 123;
                    randomChar = (char)randNum;
                 }
              }
           }
           outFile.println(password);

        }
     }
     System.out.print("\nEnter Selection (1-5): ");
     choice = in.nextInt();  
  }
  
  System.out.println("Thanks for using the Speedy Randomized Password Generator.");
  System.out.println();
  System.out.println("Please wait one moment while we retrieve your randomized passwords.");      
  
  while(inFile.hasNext() )
  {
        passwords = inFile.next("");      //read next password from file
        System.out.println(passwords);   //print value of password
  }//end of while loop

} }




Aucun commentaire:

Enregistrer un commentaire