简体   繁体   中英

Java Programing: 2 methods to check if an input string is alphanumeric and if the input string has a prefix of the user's name in it

Hi all the two methods below appear to be misbehaving. I was wondering if you guys can help me find out why ?

Method 1: boolean isAlphanumeric(String key)

*This method is supposed to check if the String variable "key" has AT LEAST one letter and one number. If this is true, then the method is supposed to return the boolean value TRUE. However it does not and returns the value of FALSE instead.

The first for loop is mean't to check if there is at least one character in the String variable "key" that contains a letter. The exits the loop and enters the second for loop which is supposed to check if there are any characters in the String variable 'key' that is a digit or number.

The if statement at the end then checks to see of there is BOTH at least 1 letter and 1 number in the String variable 'key'.*


Method 2: boolean containsNamePrefix(String key, String name)

*This method is meant to check if there is a prefix of at least 2 or more consecutive characters starting from the first letter found in the String variable 'name' that is inside the String variable 'key'

For example:

String key = "Johnny_Cash"; String name = "Johnny";

the method is then supposed to return a boolean value of TRUE since "Jo" is a prefix of 2 characters (at least 2 needed) in the String variable 'name' that can ALSO be found in the String variable 'key'. The following will also return a value of true: Joh, John, Johnn, Johnny.

Again this method does not return the boolean value of TRUE unfortunately...*

HELP !


public class main {
    public static void main(String[] args) {
        String key = "Johnny_Cash";
        String name = "Joh7";

        System.out.println(isAlphanumeric(key));
        System.out.println(containsNamePrefix(key, name));
    }

public static boolean isAlphanumeric(String key) {
    char currentCharacter;
    boolean letterPresent = false;
    boolean numberPresent = false;
    boolean isAlphanumeric = false;
    for(int i = 0; i < key.length(); ++i) {
        currentCharacter = key.charAt(i);
        if(Character.isLetter(currentCharacter)) {
            letterPresent = true;
            break;
        }
    }
    for(int i = 0; i < key.length(); ++i) {
        currentCharacter = key.charAt(i);
        if(Character.isDigit(currentCharacter)) {
            numberPresent = true;
            break;
        }               
    }
    if(letterPresent && numberPresent) { // at least one letter and one number in key
        isAlphanumeric = true;
    }
    return isAlphanumeric;      
}

public static boolean containsNamePrefix(String key, String name) {
    boolean keyContainsPrefix = false;
    String prefix;
    for(int i = 0; i < name.length(); ++i) {
        prefix = name.substring(0, i+1);
        if(key.contains(prefix)) {
            keyContainsPrefix = true;
            break;
        }
    }                 
    return keyContainsPrefix;
}

}

OUTPUT:

false true

/* EDIT: Hey guys sorry here's the FULL code snippet as requested. Sorry guys method 2 seems to be working fine now but method one is till outputting false. */

EDIT 2: Hey guys sorry code is working like a charm.. turns out Ali was right. I just needed a nap...

For question 1: I don't see anything wrong here. Can you not debug the method with a short string, eg "a1"? You can simply run it step by step and should easily find the error.

Btw: the method can be vastly simplified, but that's not the point here, I guess.

public static boolean containsNamePrefix(String key, String name) {
    boolean keyContainsPrefix = false;
    String prefix;
    for(int i = 0; (i+1) < name.length(); ++i) { // 1. change here
        prefix = name.substring(i, i+2); // 2. increment your substring
        if(key.contains(prefix)) {
            keyContainsPrefix = true;
            break;
        }
    }
    return keyContainsPrefix;
}

Your both methods are fine regarding the specifications of each! The problem might be in how you are handling the result of each calls, can you share the part of code where you are calling them.

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