You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java文件加解密程序:如何移除加密密钥中的重复字符?

How to Remove Duplicate Characters from Your Encryption Key in 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 processedKey variable now holds your key with all duplicates stripped (e.g., "FEATHER" becomes "FEATHR").
  • I renamed your class from Average to something more descriptive, but you can revert that if you prefer.
  • Always close your Scanner and PrintWriter resources to avoid resource leaks.

内容的提问来源于stack exchange,提问作者MidnightMenace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:54:31