Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java String Maniplulation

Name: Anonymous 2011-05-22 22:37

this doesn't work if the first letter has a duplicate at the end. what am i doing wrong?

    /**
     * Removes duplicate characters. Only latter instances<br/>
     * are deleted
     * @param s - the original
     * @return the string with dupes removed
     */
    public static String removeDuplicates(String s){
        for (int x = 0; x < s.length(); x++){
            for (int k = x + 1 ; k < s.length(); k++){
                if (s.charAt(x) == s.charAt(k))
                    s = s.substring(0, k) + s.substring(k + 1);
            }
        }
        return s;
    }

Name: Anonymous 2011-05-22 22:38

"Maniplulation"

please disregard the retard moment I had

Name: Anonymous 2011-05-22 22:52

Try not mutating your variables

or use a real language like Haskell

Name: Anonymous 2011-05-22 23:02

>>3

I know it's not a good practice, but that doesn't affect the functionality of it.

Name: Anonymous 2011-05-22 23:22

>>4
you dont want x going to s.length() but s.length()-1

Name: Anonymous 2011-05-22 23:25

>>5
nope, still doesn't work.

is s = s.substring(0, k) + s.substring(k + 1); correct? I'm 99% sure that it is...

Name: Anonymous 2011-05-22 23:30

>>1
It's pretty simple. When the duplicate is at the end, evaluating s.substring(k + 1)[/string] breaks because [code]k + 1 > s.length. You can work around this in various ways, but the lazy way that first came to my mind is changing that line to:

s = s.substring(0, k) + (k == s.length - 1 ? s.substring(k + 1) : "");

Name: >>7 2011-05-22 23:32

>>7 s/[\/string]/[\/code]

Sorry, I just fapped and my mind is not working...

Name: Anonymous 2011-05-22 23:34

It's pretty simple. When the duplicate is at the end, evaluating s.substring(k + 1) breaks because k + 1 > s.length. You can work around this in various ways, but the lazy way that first came to my mind is changing that line to:

s = s.substring(0, k) + (k == s.length - 1 ? s.substring(k + 1) : "");

Name: Anonymous 2011-05-22 23:44

>>9
I think I know what you're saying, but since k will eventually reach s.length - 1 in every iteration of the inner loop, it will always do
s = s.substring(0, k) + "");
which basically deletes everything but the first letter.

Name: Anonymous 2011-05-22 23:56

>>10
Have you actually tried it yet?

Name: Anonymous 2011-05-23 0:02

>>10
public static String removeDuplicates(String s){
    for (int x = 0; x < s.length(); x++){
        for (int k = x + 1 ; k < s.length(); k++){
            [b][i][u][o]if (s.charAt(x) == s.charAt(k))[/o][/u][/i][/b] // THEN WHAT IS THIS FUCKING SHIT DOING HERE, RETARD??
                s = s.substring(0, k) + (k == s.length - 1 ? s.substring(k + 1) : "");
        }
    }
    return s;
}

Name: >>12 2011-05-23 0:03

Nothing will cover my BBCode shame, but...

public static String removeDuplicates(String s){
    for (int x = 0; x < s.length(); x++){
        for (int k = x + 1 ; k < s.length(); k++){
if (s.charAt(x) == s.charAt(k)) // THEN WHAT IS THIS FUCKING SHIT DOING HERE, RETARD??
                s = s.substring(0, k) + (k == s.length - 1 ? s.substring(k + 1) : "");
        }
    }
    return s;
}

Name: Anonymous 2011-05-23 0:06

>>13
This is what you get for helping him!

Name: Anonymous 2011-05-23 0:13

>>14
Indeed. I learned my lesson. That is, do not help a Java code monkey do his fill-in-the-blanks homework ever again. I am wholeheartedly sorry, my fellow /prog/lodytes. I bitterly repent while soaking in regret and shame, wishing for nothing more than your forgiveness.

Name: Anonymous 2011-05-23 0:15

what am i doing wrong?
- You're using Java
- You're using stereotypical, university-taught, overly mundane documentation
- You're following the path of the code monkey to-be

Happy to help!

Name: Anonymous 2011-05-23 2:07

>>13
what is wrong with this if statement? it checks if the letters i'm comparing are the same...

>>16
i'm going to teach myself C++ this summer.
how do i avoid the path of the code monkey?

Name: Anonymous 2011-05-23 2:32

Name: bored 2011-05-23 2:38

Here is a really sloppy (and ugyly) java solution that I came up with on the spot...

http://pastebin.com/aB6P0z1j

And here is what I get when I run it...

run:
Enter a string: fuck
fuck
BUILD SUCCESSFUL (total time: 3 seconds)

run:
Enter a string: xxxeeffeddddc
xefdc
BUILD SUCCESSFUL (total time: 7 seconds)

run:
Enter a string: test
tes
BUILD SUCCESSFUL (total time: 6 seconds)

run:
Enter a string: tttemopu
temopu
BUILD SUCCESSFUL (total time: 6 seconds)

run:
Enter a string: xefffffuk
xefuk
BUILD SUCCESSFUL (total time: 7 seconds)


run:
Enter a string: shitttttttttt
shit
BUILD SUCCESSFUL (total time: 5 seconds)

Name: Anonymous 2011-05-23 2:42

Can we please stop talking about Java, it's making me feel nauseous.

Name: Anonymous 2011-05-23 2:55

>>20
Fine. Let's go back to discussing autism.

Name: Anonymous 2011-05-23 3:12

Autism is for gays.

Name: Anonymous 2011-05-23 3:20

BUILD SUCCESSFUL (total time: ANUS seconds)

Name: Anonymous 2011-05-23 10:01

gay is for Autism

Don't change these.
Name: Email:
Entire Thread Thread List