英文:
How to check if part of a string is lowercase?
问题
我想编写一个方法来检查给定的输入是否拼写得像一个名字。
例如:输入“Jim”应该返回true(或在我的代码中是“okay”),而“pam”甚至“pAM”应该返回false(在我的代码中是“erro”)。
现在,该方法总是返回“erro”。
ch1 = name.charAt(characterIndex);
while (loop <= numChars) { // numChars是我输入的单词的长度,loop是我从哪个字母开始的。
for (int i = 97; i < 123; i++) { // 遍历所有小写字母的ASCII值。97 = a, 122 = z
if (ch1 == i) // ch1是当前正在检查的字符。
continue;
else { // 问题出在这里
answer = "Erro"; // “Erro”是“Error”的缩写,我将在代码实现中检查它
break;
}
}
if (answer != "Erro") { // 检查是否出现错误
loop++;
characterIndex++;
ch1 = name.charAt(characterIndex);
} else {
break;
}
}
if (answer != "Erro")
answer = "okay"; // 我可以在这里放任何内容,但这是当一切顺利时的情况。
return answer; // 即使我明确地给它一个'a',我仍然得到“Error”的结果。
英文:
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'
答案1
得分: 0
如果我理解你的问题正确,你想要在输入中包含大写字母时返回一个字符串,否则返回另一个字符串。为什么不直接返回一个布尔值呢?这是我解决你的问题的方法:
public static boolean containsUppercase(String input) {
if (Character.isLowerCase(input.charAt(0))) // 如果第一个字母是小写,返回false
return false;
for (int i = 1; i < input.length(); i++)
if (Character.isUpperCase(input.charAt(i)))
return true;
return false;
}
好的,以下是你的代码存在问题的一些地方:
- 你使用
==
和!=
来检查字符串是否相等。应该使用.equals(...)
来代替。 - 你的第一个 for 循环只接受小写的 'a'。
- 你在存储答案,其实可以直接存储为布尔值,但更聪明的做法是当出现问题时立即返回结果。
英文:
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论