0

I have these two methods- My code doesn't seem to be working as planned

What is is supposed to do is- Go through the array of characters- If there's not another character the same in the array, it is supposed to add itself to the index variable-

This is my comparing method

private boolean isValid(char c) {
    for(int i = 0; i < letters.length; i++) {
        if(Arrays.asList(letters).equals(c)) {
            return false; //Not valid
        }   
    } 
    return true;

Full code is below though

public void generate(String first, String second) {

    tempString = new StringBuilder(first+second).reverse();     
    letters = new char[tempString.length()];

    for(int i = 0; i < tempString.length(); i++) {
        letters[i]= tempString.charAt(i);       

        if(isValid(tempString.charAt(i))) {
            index += i;
        }           
    } 

}



private boolean isValid(char c) {
    for(int i = 0; i < letters.length; i++) {
        if(Arrays.asList(letters).equals(c)) {
            return false; //Not valid
        }   
    } 
    return true;


}
2
  • could you be more clear on what you want or where it's giving you an error? Commented Mar 25, 2016 at 17:33
  • 4
    if(Arrays.asList(letters).equals(c)) { You convert the whole array into a list and check if the whole list is equal to a single char? What? Commented Mar 25, 2016 at 17:35

2 Answers 2

3

There is no need to convert to a List (and a List is not a primitive char), you can use == for comparing primitive values (such as your chars). Something like,

private boolean isValid(char c) {
    for (int i = 0; i < letters.length; i++) {
        if (letters[i] == c) {
            return false;
        }
    } 
    return true;
}

or with an enhanced for-each loop like

private boolean isValid(char c) {
    for (char letter : letters) { // <-- for each letter in letters.
        if (letter == c) {        // <-- if the letter is equal to the argument.
            return false;
        }
    } 
    return true;
}

You should also test for validity before adding to the array like

if (isValid(tempString.charAt(i))) {
    letters[index] = tempString.charAt(i);       
    index++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If i test for Validity before- It doesn't store all the characters in the array- I need all the characters - If a character matches, i am supposed to return the index which the first character had- say if i input heh It should return index = 1 because 0 for h - 1 for e - 0 for h again
Then you should return the index instead of a boolean from isValid (return -1 if it isn't found, and check for that condition).
1

Arrays.asList(letters).equals(c) is comparing the list to the character (Is the list equal to this character which is not what you want.

To find if a character is in the string you can instead do

string.indexOf('a') which will return -1 if the character is not present and >= 0 if it is in the string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.