0

My code takes a user inputted string and returns the number of words as well as the first word. When the user inputs an empty string I don't want "Your string has x words in it" or "The first word is x" to display so I created a boolean but the boolean is not being set within the method I tried to set it to true in. Any Help I can get on why or how to fix it would be great. Thanks!

public static void main(String[] args){
    boolean empty = false;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String str = in.nextLine();

    if (empty == false) {
        System.out.println("Your string has " + getWordCount(str)+" words in it.");
        System.out.println("The first word is: " + firstWord(str));
    }
}

public static String firstWord(String input) {

    for(int i = 0; i < input.length(); i++)
    {
        if(input.charAt(i) == ' ')
        {
            return input.substring(0, i);
        }
    }

    return input; 
}    

 public static int getWordCount(String str){
       int count = 0;

       if(str != null && str.length() == 0){ 
           System.out.println("ERROR - string must not be empty.");
           empty = true;
       }

       else if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))){

            for (int i = 0; i < str.length(); i++){

                if (str.charAt(i) == ' '){
                    count++;
                }
            }
            count = count + 1; 
        }
    return count;
    }
 }
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Ivy Legg
  • 3
  • 2
  • 1
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – GBlodgett Oct 03 '18 at 18:04
  • You are forgetting to re-set the empty to str.length() == 0 – fasaas Oct 03 '18 at 18:05
  • it is another problem. the scope of boolean different?!! – Amin Oct 03 '18 at 18:06
  • 1) This will not compile. `empty` is not in scope in your method. 2) Java is pass by value, which means even if you do pass `empty` to the method, you are simply passing the *value* of `empty` not the actual variable. – GBlodgett Oct 03 '18 at 18:07
  • Also you need to rethink your logic here a little. You are checking if `empty` is true *before* you call `getWordCount()`, which will set `empty` to true – GBlodgett Oct 03 '18 at 18:10

3 Answers3

0

You have to re-think your logic here (see below snippet):

  • First off: you don't need the empty variable
  • You can know if the "word" is empty by calling the getWordCount method and store the result in a variable (wordCount?). Then you can check if there is at least one word by doing wordCount > 0.

The snippet:

    public static void main(String[] args){
        // boolean empty = false;           // --> not needed
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();
        final int wordCount = getWordCount(str);

        if (wordCount > 0) { // show message only if there is at least one word
            System.out.println("Your string has " + wordCount +" words in it.");
            System.out.println("The first word is: " + firstWord(str));
        }
    }

    public static String firstWord(String input) {
        // .. code omitted for brevity
    }

    public static int getWordCount(String str){
        int count = 0;

        if(str != null && str.length() == 0){
            System.out.println("ERROR - string must not be empty.");
            // empty = true; -->  not needed
        }

        // ... code omitted for brevity
        return count;
    }
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
0

you just need to change the place if for the sake of variable visibility.

public static void main(String[] args) {
        boolean empty = false;
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();

        if (str != null && str.length() == 0) {
            System.out.println("ERROR - string must not be empty.");
            empty = true;
        }

        if (empty == false) {
            System.out.println("Your string has " + getWordCount(str) + " words in it.");
            System.out.println("The first word is: " + firstWord(str));
        }
    }

public static String firstWord(String input) {

        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == ' ') {
                return input.substring(0, i);
            }
        }

        return input;
    }

public static int getWordCount(String str) {
        int count = 0;

        if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))) {

            for (int i = 0; i < str.length(); i++) {

                if (str.charAt(i) == ' ') {
                    count++;
                }
            }
            count = count + 1;
        }
        return count;
    }
phdias
  • 205
  • 1
  • 11
0

If you would really want to use a boolean, try creating a boolean method using a string parameter, which returns true only if the string is not empty, then assign the boolean return type to an empty boolean variable.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Tega Steve
  • 11
  • 1