JAVA problem

Sharky Forums


Results 1 to 8 of 8

Thread: JAVA problem

  1. #1
    Reef Shark chronicle's Avatar
    Join Date
    Jan 2002
    Location
    NYC, NY, USA
    Posts
    311

    Arrow JAVA problem

    Hi

    Having some trouble with JAVA.

    I'm writing a little game and I need to compare an array with "chars" sort of to it self. So if I run an array with these inputs "QWERTYT" (Q is the #0 in the array, W 1 etc)it should end up with something like this "QWERTY*". SO it takes away all letters that have a duplicate. I can't get it to work, so please help.

    thanks
    Last edited by chronicle; 04-21-2003 at 09:56 AM.

  2. #2
    Expensive Sushi cjohnson's Avatar
    Join Date
    Oct 2002
    Location
    North Carolina
    Posts
    35
    Okay, I'm not sure I totally follow what you mean, but it sounds like you're just looking for duplicates in a char[] array. Just traverse the array, starting at the beginning, keep track of the character you're looking for, and just replace it with asterisks (I'm assuming that's what you want, given the example)... This is really ad hoc, but...

    Code:
    public class DuplicateElimination {
        public static char[] removeDuplicates(char[] array) {
    	char[] retVal = (char[]) array.clone();
    
    	for (int i = 0; i < retVal.length; i++) {
    	    char current = retVal[i];
    
    	    for (int j = 0; j < i; j++) {
    		if (retVal[j] == current)
    		    retVal[j] = '*';
    	    }
    
    	    for (int j = i + 1; j < retVal.length; j++) {
    		if (retVal[j] == current)
    		    retVal[j] = '*';
    	    }
    	}
    
    	return retVal;
        }
    
        public static void main(String[] args) {
    	char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
    				  'D', 'V', 'O', 'R', 'A', 'K'};
            System.out.println(DuplicateElimination.removeDuplicates(foo));
        }
    }
    Which outputs 'QWERTYDVO*AK'.
    <wik> /bin/finger that girl in the back row of machines

  3. #3
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    This does look like a good solution. But I have a couple observations. First, there's no reason to clone the input array. You can just work with the "array" variable throughout and return it at the end. Second, you don't need that first loop (int j=0; j<1; j++). You only need to loop from the current position+1 to the end (as the second loop does). Finally, if curret == '*', there's no need to run the inside loop at all.

    These suggestions worked into the original solution are:
    Code:
    public class DuplicateElimination {
        public static char[] removeDuplicates(char[] array) {
            for (int i = 0; i < array.length; i++) {
                char current = array[i];
                if (current != '*') {
                    for (int j = i + 1; j < array.length; j++) {
                        if (array[j] == current) {
                            array[j] = '*';
                        }
                    }
                }
            }
            return array;
        }
    
        public static void main(String[] args) {
            char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
                                    'D', 'V', 'O', 'R', 'A', 'K'};
            System.out.println(DuplicateElimination.removeDuplicates(foo));
        }
    }
    It's always easier to critique than to write

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  4. #4
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    Probably want to clone the input array, if you don't want the input array modified.
    Stay cool
    and be somebody's fool this year

  5. #5
    Reef Shark chronicle's Avatar
    Join Date
    Jan 2002
    Location
    NYC, NY, USA
    Posts
    311
    Originally posted by cjohnson
    Okay, I'm not sure I totally follow what you mean, but it sounds like you're just looking for duplicates in a char[] array. Just traverse the array, starting at the beginning, keep track of the character you're looking for, and just replace it with asterisks (I'm assuming that's what you want, given the example)... This is really ad hoc, but...

    Code:
    public class DuplicateElimination {
        public static char[] removeDuplicates(char[] array) {
    	char[] retVal = (char[]) array.clone();
    
    	for (int i = 0; i < retVal.length; i++) {
    	    char current = retVal[i];
    
    	    for (int j = 0; j < i; j++) {
    		if (retVal[j] == current)
    		    retVal[j] = '*';
    	    }
    
    	    for (int j = i + 1; j < retVal.length; j++) {
    		if (retVal[j] == current)
    		    retVal[j] = '*';
    	    }
    	}
    
    	return retVal;
        }
    
        public static void main(String[] args) {
    	char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
    				  'D', 'V', 'O', 'R', 'A', 'K'};
            System.out.println(DuplicateElimination.removeDuplicates(foo));
        }
    }
    Which outputs 'QWERTYDVO*AK'.
    I'm very thankful 4 your answers but what if I wanted to test it for non-capital letters also, what would you need to change?

    QWERTrDVORAr -> QWERT*DVO*A*
    or like
    Babar -> Ba**r
    Last edited by chronicle; 04-22-2003 at 03:43 PM.

  6. #6
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    http://java.sun.com/j2se/1.4.1/docs/...Character.html

    You'll find the methods of the Character class useful.
    Stay cool
    and be somebody's fool this year

  7. #7
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Originally posted by EverlastingGod
    Probably want to clone the input array, if you don't want the input array modified.
    Ah, but this wasn't part of the initial request
    Maybe I've been reading too many engagement letters recently...

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  8. #8
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    Originally posted by rock
    Ah, but this wasn't part of the initial request
    But it wasn't not part of the initial request
    Stay cool
    and be somebody's fool this year

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •