简体   繁体   中英

Eliminating repeated characters in a string

Hello I'm trying to get this piece to work so that when the input is a string such as "aaabbbccdddeef" the output is "abcdef". I know there are solutions out there but it's bothering me that this isn't working and I can't see why. I'd really appreciate it if someone could give me a hand in understanding why this piece of code doesn't work.

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    char[] store = new char[str.length()]; 
    int count =0;

    for(int i=0; i<str.length();i++) {

        for (int j=0; j<str.length(); j++) {

                if(str.charAt(i)==store[j] ){
                    count+=1;//when character not stored keep count to offset store position
                    break;
                }else {store[i-count] = str.charAt(i); count = 0;}
            }
        }
    System.out.println(str);
    System.out.print(store);

Another way would be to append to a StringBuilder if it does not exist

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    StringBuilder store = new StringBuilder ();

    for(int i=0; i<str.length();i++) {
        if (!store.toString().contains(Character.toString(str.charAt(i)))) {
            store.append(str.charAt(i));
        }
    }
    System.out.println(str);
    System.out.print(store);

Need to change the logic inside the second for loop. you have to iterate store not the str in the second for loop. Check my solution:

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: ");
    String str = input.nextLine();
    char[] store = new char[str.length()];
    int count = 0;
    boolean charInStore = false;
    for (int i = 0; i < str.length(); i++) {
        charInStore = false;
        for (int j = 0; j < store.length; j++) {
            if (str.charAt(i) == store[j]) {
                charInStore = true;
                break;
            }
        }
        if (!charInStore) {
            store[count] = str.charAt(i);
            count++;
        }
    }
    System.out.println(str);
    System.out.println(new String(store).trim());

You really need to make use of desk checking to get a better understanding of what your code is doing...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
|    0 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    1 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    2 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    3 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    4 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    5 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    1 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    2 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     2 |
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
|    5 |    0 | aaabbb | [a, b,  , b, b,  ] |     0 |
|    5 |    1 | aaabbb | [a, b,  , b, b,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

The core problem is store[i - count] = str.charAt(i); . It might not be obvious until you understand what's going on.

Let's take a closer look at the point things start going wrong...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

Ok, when i = 4 and j is 0

  • str.charAt(i) = b
  • store[j] = a
  • count = 0

So, b != a , so you use store[i - count] , which equates to store[4 - 0] and store str.charAt(i) (or b ) at that point

And things just spiral out of control from there.

The "basic" problem is count has no relevance between loops. I'd also question the need for two loops anyway

This can be a simple solution it can also be a very complex solution.

My solution is very simply in my opinion: Make a for loop that iterates through the entire length of the string Then use index of + charAt to determine if a string repeats over 1 Create new String called temp. Then if so remove every single one of those characters leaving only one Then print temp

String.indexOf('a');

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