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.
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(