I have the following 3 Methods:
Method: Takes a text and splits its words in particular Strings and puts them into a String Array.
public static String[] generateStringArray(String text) {
if (text.isEmpty()) {
throw new IllegalArgumentException(MSG_STRING_EMPTY);
}
// split pattern for all non word characters
return text.trim().split("\\W+");
}Method: Counts the amount of Strings in an Array that consist solely out of Upper-Case or Lower-Case Letters.
public static int countLowerUpperCaseStrings(String[] stringArray) {
if (stringArray.length == 0) {
throw new IllegalArgumentException(MSG_ARRAY_LENGTH_ZERO);
}
String convertedString;
int counter = 0;
char a;
char b;
for (int i = 0; i < stringArray.length; i++) {
a = stringArray[i].charAt(0);
b = stringArray[i].toLowerCase().charAt(0);
if (a == b) {
convertedString = stringArray[i].toLowerCase();
} else {
convertedString = stringArray[i].toUpperCase();
}
if (stringArray[i].compareTo(convertedString) == 0) {
counter++;
}
}
return counter;
}Method: The actual method, where you enter a text via Scanner and call for the other methods.
public void numberOfUpperLowerCaseStrings() {
String text;
String[] stringArray;
System.out.println("Enter a text:");
text = input.nextLine();
stringArray = Algorithmen.generateStringArray(text);
System.out.println(Algorithmen.countLowerUpperCaseStrings(stringArray));
}
Note that Algorithmen is the name of the class where the first two (static) methods are defined.
I tested the first two methods and they work just fine.
Now the problem comes into play, if I call the third method to firstly generate a text through the Scanner
.
But somehow, the code just skips the line text = input.nextLine(); and throws the Exception (MSG) String is empty. The text "enter a text:" is executed, directly followed by the exception. But I am not able to enter any text.
if I change input.nextLine();
into input.next(), I am able to enter a text but after that, end up in an endless loop of exceptions.
So why can't I enter a text?
I have another two methods where input.nextLine
works just fine.
No comments:
Post a Comment