0

I have a program where I have encrypt or decrypt a file. The keyword is a user-entered string but any repeating letters have to be removed.

For example, FEATHER would become FEATHR.

I currently have the following application as a starting point, but I don't know how to remove the duplicate letters. What do I need to change or add?

import java.io.File;
import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;


public class Average

{

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.ed);
    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 = in.next();
Dylan Knowles
  • 2,726
  • 1
  • 26
  • 52
  • Well, how far have you gotten in finding the first letter E? Also, you only need a single Scanner object, by the way, while `System.ed` doesn't exist – OneCricketeer May 16 '18 at 06:20
  • 1
    The above duplicate link looks a good solution here. Also, you could approach this at the UI level, namely by preventing a user from entering a letter which has already been entered. This might also be more intuitive than allowing them to enter whatever they want. – Tim Biegeleisen May 16 '18 at 06:23
  • thanks i'll use what youve said – MidnightMenace May 16 '18 at 06:25

1 Answers1

-1

You can use Set collection to store the chars from the char sequence, Set allows only unique entries, so you will be golden. Example:

@Test
public void setTest(){
  Set<Integer> integerSet = new LinkedHashSet<>();
  integerSet.add(1); // return true, operation successfull
  integerSet.add(2); // return true
  integerSet.add(1); // return false
  asserEquals(2, integerSet.size()); // test is green, passed
}

As guys mentioned in comments and suggestions that's true, we need to preserve ordering and not all Set implementations do that. Thank you guys.

  • 1
    `so you will be golden`. Unless you use a set which preserves insertion order, such as `LinkedHashSet`, you will erase the original ordering of letters before the duplicates were removed. See the duplicate link for a better explanation. – Tim Biegeleisen May 16 '18 at 06:28