Java文件加解密程序:如何移除加密密钥中的重复字符?
Hey there! Let's fix that duplicate character issue for your encryption key. I'll walk you through the implementation step by step, plus fix a couple of small typos in your existing code.
First, the Core Logic: Removing Duplicates
There are two straightforward ways to strip duplicate characters from your key string. Both will preserve the original order of characters (which is critical for encryption consistency):
Method 1: Using StringBuilder with Index Check
This simple approach checks each character against the ones we've already added to a StringBuilder:
private static String removeDuplicates(String input) { StringBuilder uniqueChars = new StringBuilder(); for (char c : input.toCharArray()) { // Only add the character if it hasn't been included before if (uniqueChars.indexOf(String.valueOf(c)) == -1) { uniqueChars.append(c); } } return uniqueChars.toString(); }
Method 2: Using HashSet (More Efficient)
For longer keys, this method is faster because HashSet checks for duplicates in constant time:
import java.util.HashSet; import java.util.Set; private static String removeDuplicates(String input) { Set<Character> seenChars = new HashSet<>(); StringBuilder uniqueChars = new StringBuilder(); for (char c : input.toCharArray()) { // HashSet.add() returns false if the character already exists if (seenChars.add(c)) { uniqueChars.append(c); } } return uniqueChars.toString(); }
Integrating This into Your Existing Code
Here's your updated code with the duplicate removal logic added, plus fixes for typos (like System.ed → System.in and in.next() → console.next()):
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class FileEncryptorDecryptor { // Renamed for clarity (feel free to change back) public static void main(String[] args) throws FileNotFoundException { System.out.print("Would you like to encrypt or decrypt. e/d: "); Scanner ed = new Scanner(System.in); // Fixed System.ed typo String eOrD = ed.next(); if (eOrD.equals("e")) { Scanner console = new Scanner(System.in); System.out.print("Output File: "); String outputFileName = console.next(); PrintWriter out = new PrintWriter(outputFileName); System.out.print("Please enter an encryption key: "); String keyWord = console.next(); // Fixed in.next() typo // Process the key to remove duplicates String processedKey = removeDuplicates(keyWord); System.out.println("Processed key (duplicates removed): " + processedKey); // Add your encryption logic here using processedKey // ... // Clean up resources out.close(); console.close(); } ed.close(); } private static String removeDuplicates(String input) { Set<Character> seenChars = new HashSet<>(); StringBuilder uniqueChars = new StringBuilder(); for (char c : input.toCharArray()) { if (seenChars.add(c)) { uniqueChars.append(c); } } return uniqueChars.toString(); } }
Quick Notes:
- The
processedKeyvariable now holds your key with all duplicates stripped (e.g., "FEATHER" becomes "FEATHR"). - I renamed your class from
Averageto something more descriptive, but you can revert that if you prefer. - Always close your
ScannerandPrintWriterresources to avoid resource leaks.
内容的提问来源于stack exchange,提问作者MidnightMenace

