英文:
Java Input Validation, only loops twice, not detecting Upper/Lower Case Characters
问题
以下是你的代码的翻译部分:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class charEvaluation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> tokenizedInput = new ArrayList<>();
String sentenceRetrieved;
// 获取用户输入的句子
System.out.println("请输入至少含有4个单词、最多含有8个单词的句子:");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);
// 检查确保字符串含有4-8个单词
while (strTokenizer.hasMoreTokens()) {
if (strTokenizer.countTokens() > 8 || strTokenizer.countTokens() < 4) {
System.out.println("请重新输入至少含有4个单词、最多含有8个单词的句子:");
sentenceRetrieved = sc.nextLine();
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
} else {
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
System.out.println("谢谢。");
break;
}
}
// 打印句子
System.out.println("你输入的句子为:");
System.out.println(sentenceRetrieved);
// 统计每个单词中的字符数
int totalLength = 0;
for (String each : tokenizedInput) {
totalLength += each.length();
System.out.println(each + " 含有 " + each.length() + " 个字符。");
}
System.out.println("输入的字符总数(不含空格):" +
sentenceRetrieved.replace(" ", "").length());
/*
* 设置字符数组并确定输入中有多少个字母、数字、小写字母、大写字母和空格。
*/
char[] array;
int letters = 0,
digits = 0,
lowerCase = 0,
upperCase = 0,
whitespaces = 0;
array = sentenceRetrieved.toCharArray();
for (int i = 0; i < array.length; i++) {
if (Character.isLetter(array[i]))
letters++;
else if (Character.isDigit(array[i]))
digits++;
else if (Character.isUpperCase(array[i]))
upperCase++;
else if (Character.isLowerCase(array[i]))
lowerCase++;
else if (Character.isWhitespace(array[i]))
whitespaces++;
}
System.out.println("字母的数量为:" + letters + "。");
System.out.println("数字的数量为:" + digits + "。");
System.out.println("小写字母的数量为:" + lowerCase + "。");
System.out.println("大写字母的数量为:" + upperCase + "。");
System.out.println("空格的数量为:" + whitespaces + "。");
}
}
注意:代码翻译只涉及代码部分的翻译,而注释内容保持原样。如果你有关于代码逻辑或翻译的任何问题,请随时问我。
英文:
I want my input to be a minimum of 4 strings and a maximum of 8. I am tokenizing them for practice. My loop will rerun if I the input is outside of these parameters, but will only rerun once and continue on with the rest of the program. I feel like I am using the wrong loop for validation because I would just have while/if to infinity at this point I think.
Finally, at the end, when trying to determine how many chars are upper or lower case, my results returns a count of 0, but my letters, digits and white spaces counts are accurate.
I really appreciate any insight.
Here is my code:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class charEvaluation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> tokenizedInput = new ArrayList<>();
String sentenceRetrieved;
// getting the sentence from the user
System.out.println("Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);
// checking to ensure the string has 4-8 words
while (strTokenizer.hasMoreTokens()) {
if (strTokenizer.countTokens() > 8 || strTokenizer.countTokens() < 4) {
System.out.println("Please re-enter a sentence with at least 4 words, and a maximum of 8: ");
//sentenceRetrieved = null;
//tokenizedInput.removeAll(tokenizedInput);
sentenceRetrieved = sc.nextLine();
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
} else {
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
System.out.println("Thank you.");
break;
}
}
// printing out the sentence
System.out.println("You entered: ");
System.out.println(sentenceRetrieved);
// count the characters in each word
int totalLength = 0;
for (String each : tokenizedInput) {
totalLength += each.length();
System.out.println( each + " has " + each.length() + " characters.");
}
System.out.println("The total number of characters entered without spaces: "+
sentenceRetrieved.replace(" ", "").length());
/*
* Setting up a character array and determining how many
* letters, digits, lower case letters, upper case letters and white spaces in the input.
*/
char [] array;
int letters = 0,
digits = 0,
lowerCase = 0,
upperCase = 0,
whitespaces = 0;
array = sentenceRetrieved.toCharArray();
for(int i = 0; i< array.length; i++) {
if (Character.isLetter(array[i]))
letters ++;
else if(Character.isDigit(array[i]))
digits++;
else if(Character.isUpperCase(array[i]))
upperCase++;
else if(Character.isLowerCase(array[i]))
lowerCase++;
else if(Character.isWhitespace(array[i]))
whitespaces++;
}
System.out.println("The number of letters is " + letters + ".");
System.out.println("The number of digits is " + digits + ".");
System.out.println("The number of lower case letters is " + lowerCase+ ".");
System.out.println("The number of upper case letters is " + upperCase + ".");
System.out.println("The number of white spaces is " + whitespaces + ".");
}
}
My output then looks like this:
Please type a sentence containing at least 4 words, with a maximum of 8 words:
hi there3
Please re-enter a sentence with at least 4 words, and a maximum of 8:
hi there3
You entered:
hi there3
hi has 2 characters.
there3 has 6 characters.
The total number of characters entered without spaces: 8
The number of letters is 7.
The number of digits is 1.
The number of lower case letters is 0.
The number of upper case letters is 0.
The number of white spaces is 1.
答案1
得分: 1
如果是isLetter
,它可以是大写或小写。试试这个。
if (Character.isLetter(array[i])){
letters++;
if(Character.isUpperCase(array[i]))
upperCase++;
else if(Character.isLowerCase(array[i]))
lowerCase++;
} else if(Character.isDigit(array[i])){
digits++;
} else if(Character.isWhitespace(array[i])){
whitespaces++;
}
在你的代码中,你只在!isCharacter
的情况下检查了isUpper
或isLower
,我认为这是错误的。
这就是为什么你的计数不正确。
英文:
If isLetter
, it can be upper or lower. Try this.
if (Character.isLetter(array[i])){
letters ++;
if(Character.isUpperCase(array[i]))
upperCase++;
else if(Character.isLowerCase(array[i]))
lowerCase++;
} else if(Character.isDigit(array[i])){
digits++;
} else if(Character.isWhitespace(array[i]))
whitespaces++;
In your code, you checked if isUpper
or isLower
only if !isCharacter
which is wrong I believe.
That is why your counts were not correct
答案2
得分: 1
这样怎么样? 它会持续提示,直到满足条件。
while (true) {
System.out.println(
"请输入包含至少4个单词、最多8个单词的句子:");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer =
new StringTokenizer(sentenceRetrieved);
int count = strTokenizer.countTokens();
if (count <= 8 && count >= 4) {
break;
}
}
要解决你的第二个问题,去掉第一个 else。一旦确定是一个字母,就不需要进行其他测试。你还可以在确定是字母后,只进行大小写测试。
if (Character.isLetter(array[i]))
letters++;
if (Character.isDigit(array[i])) {
..
} else if (
英文:
How about something like this. It will keep prompting until the conditions are met.
while (true) {
System.out.println(
"Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer =
new StringTokenizer(sentenceRetrieved);
int count = strTokenizer.countTokens();
if (count <= 8 && count >= 4) {
break;
}
}
To fix your second problem, get rid of the first else. Once you determine it is a letter, you don't do any other testing. You can also just test for upper/lower once you figure out it's a letter.
if (Character.isLetter(array[i]))
letters++;
if (Character.isDigit(array[i])) {
..
} else if(
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论