删除字符串中的重复字符(用户输入的关键字) [英] Removing duplicate characters in a String (user inputted keyword)

查看:180
本文介绍了删除字符串中的重复字符(用户输入的关键字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private String removeDuplicates(String userKeyword){
  int wordLength = userKeyword.length();
  int lengthCounter;
  for (lengthCounter=0; lengthCounter<wordLength; lengthCounter++){
    if (userKeyword.charAt(lengthCounter) != userKeyword.charAt(lengthCounter + 1)){
      String revisedKeyword = "" + userKeyword.charAt(lengthCounter);
      userKeyword = revisedKeyword;

    }

}
  return userKeyword;
}

我真的很新的java,我们没有使用String构建器,Strung缓冲区,数组等....我们甚至没有得到循环,但我认为这将是最简单的方法...请帮助。

Im really new to java, we havent used String builders, Strung buffer,, Arrays, etc yet.... we havent even gotten to loops but i figured it ll be the easiest way to use... Please help.

推荐答案

有很多方法可以做到这一点。在您建立的工具集的范围内找到自己的解决方案是学习程序的全部。这是一个可能的解决方案,让您思考:

There are infinitely many ways to do this. Finding your own solution within the bounds of the toolset you've established is what learning to program is all about. Here is one possible solution to get you thinking:

创建一个集合,默认情况下只能保存唯一值

Create a Set, which by default can only hold unique values

Set<Character> mySet = new HashSet<Character>();

然后循环使用String中的字符,将每个添加到mySet

Then loop over the characters in your String, adding each to mySet

mySet.add(c);

完成后,您的集合将具有唯一且按顺序排列的字符列表,您可以使用

When you're done, your set will have a list of characters that are unique and in order, you can print them out with

for (char c : mySet) {
    System.out.println(c)
}

编辑:

这是使用嵌套循环的简单设置

Here is a simple set up using nested loops

String s = "einstein"; //This is the word you will look for duplicates in
String temp = ""; //In this string, you will add characters that are not duplicates
boolean isDuplicate = false; //This will reset every out iteration

我正在使用一个很难的例子来保持简单。请理解,String temp 将开始为空,但是当您的整个过程完成时,您的目标是具有 temp 与爱因斯坦没有重复的字符相同。如$ code> stein

I'm using a hard example to keep things simple. Please understand that the String temp will start empty, but when your whole process is done, your goal is to have temp have the same characters as einstein without duplicates. Something like stein

public static void main (String[] args) {

    String s = "einstein";
    String temp = "";
    boolean isDuplicate = false;

    for (int i = 0; i < s.length(); i++) {
        isDuplicate = false;
        char comparisonChar = s.charAt(i);
        for (int j = i + 1; j < s.length(); j++) {
            char nextChar = s.charAt(j);
            if (comparisonChar == nextChar) isDuplicate = true;
        }
        if (!isDuplicate) temp = temp + comparisonChar;
    }

    System.out.println(temp); //should print `stein`
}

}

现在我没有测试过,所以它有可能有错误,但在心理上穿行它,并尝试了解它。问我困惑。

Now I haven't tested this so it's likely it has bugs, but walk through it mentally and try to understand it. Ask me when confused.

这篇关于删除字符串中的重复字符(用户输入的关键字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆