0

I want to write a method that checks if a given input is spelled like a first name.

For example: The input "Jim" should return true (or in my code "okay"), while "pam" or even "pAM" should return false ("erro" in my code).

Right now, the method always just returns "erro".

ch1 = name.charAt(characterIndex);

while (loop <= numChars) { // numChars is the length of the word I put in, loop being what letter I start at.
    for (int i = 97; i < 123; i++) { // goes through the ASCII values for all the lowercase letters. 97 = a, 122 = z {
        if (ch1 == i) // ch1 is the character that is currently being checked. 
            continue;
        else { // THE ISSUE
            answer = "Erro"; // "Erro" is short for "Error" which I will check for in my implementing of the code
            break;             
        }
    }

    if (answer != "Erro") { // checking if I get an error or not
        loop++;
        characterIndex++;
        ch1 = name.charAt(characterIndex);
    } else {
        break;
    }
}

if (answer != "Erro")
    answer = "okay"; // I could have put anything here, but this is when things go right.

return answer; // I keep getting the result of Error, even when I explicitly give it an 'a'
cegredev
  • 1,485
  • 2
  • 11
  • 25
Jaeden
  • 25
  • 4
  • Please explain in more detail what your goal is and what problems you are facing. Do you want to write a method that checks whether there are any uppercase letters in a String? – cegredev Apr 05 '20 at 10:02
  • 1
    One issue I see your String comparison is not correct. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – ImtiazeA Apr 05 '20 at 10:02
  • Try to debug, to see clearly, but basically you're checking all lowercase, even if they're not in your input, try to reverse your check and change finals check too as suggested, starting from input chars and checking their ascii code or just do StringUtils.equals(input, StringUtils.lowercase(input)) – WoAiNii Apr 05 '20 at 10:05
  • My goal is to spell check a first name. I always get the result of "error", in my code being "erro". The code itself doesn't have syntax or compiling issues. It is a logic error. – Jaeden Apr 05 '20 at 10:23
  • Matteo Zanini. I didn't realise that. Thank you! – Jaeden Apr 05 '20 at 10:24
  • You can not compare strings with `==` or `!=`. To compare strings use `String equals(String other)`, or in your case `if ( ! "Erro".equals(answer) )` – HomeIsWhereThePcIs Apr 05 '20 at 10:24

1 Answers1

0

If I understand your question correctly, you want to return one string if the input contains uppercase letters and another if it doesn't. Why not just return a boolean instead? This is how I would solve your question:

public static boolean containsUppercase(String input) {
    if(Character.isLowerCase(input.charAt(0))) // If the first letter is lowercase return false
        return false;

    for(int i = 1; i < input.length(); i++)
        if(Character.isUpperCase(input.charAt(i)))
            return true;

    return false;
}

Okay, here are some of the things wrong with your code:

  • You use == and != to check for string equality. Use .equals(...) instead.
  • Your first for-loop only accepts lowercase 'a's.
  • You are storing an answer which you could just store as a boolean, but it would by smarter just to return the result instantly when something goes wrong.
cegredev
  • 1,485
  • 2
  • 11
  • 25
  • 1
    Thank you, but I want to deal with the issue through the logic I used, because I want to know how to use if statements and loops more correctly. – Jaeden Apr 05 '20 at 10:19
  • That is a very good mindset! However, there are many problems with your approach and I feel like you are not going to get it working that way. – cegredev Apr 05 '20 at 10:21
  • @Jaeden I've added the most important problems with your code. – cegredev Apr 05 '20 at 10:26