I want my input to be a minimum of 4 strings and a maximum of 8. I am tokenizing them for practice. I have parameters set up so that the number of tokens must be over 4 and less than 8, but my code runs no matter the number of tokens and I don't know why.
I have also tried to insert a break at multiple locations and I cannot figure out how to stop it and continue on. Each time I run it I need to manually terminate it.
I want to get the input, reprint it, list the number of chars in each token, then reevaluate the chars again for upper/lower cases, digits, white spaces.
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);
while (true) {
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
int count = strTokenizer.countTokens();
if (count <= 8 && count >= 4) {
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 ++;
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 to console then is a continual loop of the following, before I manually terminate it:
You entered:
hi there
hi has 2 characters.
there has 5 characters.
The total number of characters entered without spaces: 7
The number of letters is 7.
The number of digits is 0.
The number of lower case letters is 7.
The number of upper case letters is 0.
The number of white spaces is 1.
All of the information is accurate, but I don't want it to accept a count of less than 4 tokens or more than 8 and I then want it to stop after this output is provided once.
I really appreciate any insight.
The issue is that you have to stop your main for loop somehow; the easiest way to fix it is introduce boolean variable that will indicate to the application that loop should be terminated as such:
public static void main(String[] args) {
ArrayList<String> tokenizedInput = new ArrayList<>();
String sentenceRetrieved = "";
boolean isConditionsMet = false;
while (!isConditionsMet) {
Scanner sc = new Scanner(System.in);
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 <= 4 || count >= 8) {
continue;
} else {
isConditionsMet = true;
}
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
}
// printing out the sentence
System.out.println("You entered: ");
System.out.println(sentenceRetrieved);
// count the characters in each w
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++;
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 + ".");
}
Also, small note regarding your code - totalLength is never used.