简体   繁体   中英

Write password to txt file with simple encryption with StringBuilder and PrintWriter, (Not StringWriter)

So before i explain my question, I already asked if i could use StringWriter for this code, however we were told we had to use StringBuilder. The current Instructions state...

Once passwords match, using StringBuilder, write the password to a text file called Lab5.txt, but use the following simple encryption – write the ASCII value for each character, delimited by a comma. Write a 0 (zero) to indicate the end of the password.

I have a general idea of how this would function, however i am not sure at all where to go from here, since i am very unfamiliar with StringBuilder, and there have not been many helpful tutorials, and most stackoverflow posts regarding this suggest to use StringWriter.

My current code...

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;

public class Project5_Part1_Final {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Please enter password : ");
        String pass = in.nextLine();
        System.out.print("Please re-enter password: ");
        String confirm = in.nextLine();

        List<String> errorList = new ArrayList<String>();

        while (!isValid(pass, confirm, errorList)) {
            System.out.println("The password entered here  is invalid");
            for (String error : errorList) {
                System.out.println(error);
            }

            System.out.print("Please enter password : ");
            pass = in.nextLine();
            System.out.print("Please re-enter password: ");
            confirm = in.nextLine();
        }
        System.out.println("your password is: " + pass);

    }

    public static boolean isValid(String password, String confirm, List<String> errorList) {

        int passLength = password.length();
        int i;

        Pattern specailChar = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
        Pattern digitCase = Pattern.compile("[0-9 ]");
        errorList.clear();

        boolean testing = true;

        if (!password.equals(confirm)) {
            errorList.add("password and confirm password does not match");
            testing = false;
        }
        if (password.length() < 8) {
            errorList.add("Invalid Password - Password must be at least 8 characters");
            testing = false;
        }
        if (!digitCase.matcher(password).find()) {
            errorList.add("Invalid password - Password must have at least 1 number");
            testing = false;
        }
        if (!specailChar.matcher(password).find()) {
            errorList.add("Invalid password - Password must have at least one special character");
            testing = false;
        }

        for (i = 0; i < passLength; i++) {
            char character = password.charAt(i);
            int ascii = (int) character;
            System.out.print(ascii + ",");

            PrintWriter output = new PrintWriter("Lab5.txt");
            StringBuilder sb = new StringBuilder();
            sb.append(ascii);

            output.println(sb.toString());
        }
        System.out.print(" 0  ");       
        return testing;
    }
}

My current error message...

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    PrintWriter cannot be resolved to a type
    PrintWriter cannot be resolved to a type

    at Project5_Part1_Final.isValid(Project5_Part1_Final.java:66)
    at Project5_Part1_Final.main(Project5_Part1_Final.java:18)

I know im missing some important sections of code, however i have not been able to find any post or examples where StringBuilder and PrintWriter are used together like this, they all are either just PrintWriter, or PrintWriter and StringWriter. Any form of help would be greatly appreciated. Thank you.

The below only deals with the encryption and not with obtaining or validating the password.

As I understand your question, you need to replace each character of the password with its ASCII code, for example instead of A you need the number 65 (in decimal). Also, you need to separate each letter's ASCII code with a comma and finally you need to terminate the encoded password with the digit 0 (zero).

The below code achieves this.

String str = "George$Best11";
char[] letters = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (char letter : letters) {
    int code = (int) letter;
    sb.append(code);
    sb.append(',');
}
sb.append(0);
System.out.println(sb);
try (PrintWriter pw = new PrintWriter("Lab5.txt")) {
    pw.printf("%s%n", sb);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}

The example password is George$Best11 which conforms to your conditions for a legal password, ie

  • at least 8 characters
  • at least one special character ( $ )
  • at least one digit

I get the array of characters for the password string. I cast each character to an int . I append that int to the StringBuilder and immediately following the int I append a comma. Finally, after appending all the letters, I append the terminating zero.

The above code displays the following:

71,101,111,114,103,101,36,66,101,115,116,49,49,0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM