1

I have the following 3 Methods:

  1. 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+");
    }
    
  2. 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;
    }
    
  3. 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.

Matt
  • 1,298
  • 1
  • 12
  • 31
Strict
  • 195
  • 1
  • 2
  • 9
  • You probably have a previous call to input.nextInt() or something, and are wrongly assuming that it reads the EOL character after the integer. Thus the subsequent call to nextLine() only reads the end of the line, and thus returns an empty string. Remove all the irrelevant code, which is tested and works find. Show us the code wherer the scanner is defined and used. – JB Nizet Dec 12 '15 at 16:26
  • 1
    Where are you initializing `input` Scanner? You are most probably reading something else like an integer before this. – Aniket Thakur Dec 12 '15 at 16:26
  • Probably a duplicate of http://stackoverflow.com/questions/16040601/why-is-nextline-returning-an-empty-string – JB Nizet Dec 12 '15 at 16:27
  • I defined the Scanner globally, outside of the methods as a static variable. I just defined another Scanner exclusively for the method and now it works. Am I right to assume that the problem is, that I declared the Scanner in a static way? I did it so I can use it in static methods. And the value of static variables is available for any method using this variable. – Strict Dec 12 '15 at 16:36
  • No, you're not right. Have you read my previous comment, and the link to the duplicate? That is probably the problem. – JB Nizet Dec 12 '15 at 16:39
  • @Strict Take a look at my solution below. – user3437460 Dec 12 '15 at 16:46
  • The problem was that I were not allowed to read in any text from begin with. So I suggest that the input variable held another value (most likely int) I passed to it in another method. Because I solved the problem once I defined a new Scanner in the method, exclusively for the method. – Strict Dec 12 '15 at 16:48

1 Answers1

0

The only reason I can think of for this behaviour is that you might have used the same Scanner object to scan for an integer earlier on (not shown in your codes).

If you did,

  • just place a input.nextLine() after that input.nextInt() to clear the buffer or

  • replace your input.nextInt() with Integer.parseInt(input.nextLine()).

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • **Remarks:** Personally, I prefer the latter approach. Try it and let me know the outcome. – user3437460 Dec 12 '15 at 16:45
  • Yes, that may was the problem. As I defined the Scanner variable in a static way, all the other methods shared this variable. When I just defined a new Scanner variable exclusively for the method, it started working fine. – Strict Dec 12 '15 at 16:50
  • The latter approach doesn't do the same thing as the former one. The former reads a localized integer, and not the latter one. For example, in the US locale, typing 123,456 will work fine with the former, but not with the latter. – JB Nizet Dec 12 '15 at 16:51