Stephan
Stephan

Reputation: 19

while loop: while sentence does not contain a word

I'm getting user input and checking to see if the word 'java' is in the sentence. I did a while loop but even when the word 'java' is in the sentence, it tells me that it's not and continues with the while loop. If I remove the while loop, everything else that I want my program to do works. here is the code:

import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

        //declare all variables
        String sentence;
        int java_index_number;
        String stop_program;
        String java;
        String Java;
        String JAVA;
        String java_capital_first_letter;
        String java_all_caps;

        //scanner to get user input
        Scanner user_input = new Scanner(System.in);

        //prompt the user for a sentence
        System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
        sentence = user_input.nextLine();

        java = "java";
        Java = "Java";
        JAVA = "JAVA";
        while(!sentence.contains(java) || !sentence.contains(Java) || !sentence.contains(JAVA)) {
            System.out.println("Your sentence does not have the word 'java' within it.");
            System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
            sentence = user_input.nextLine();
        }

        //output
        System.out.println();
        System.out.println("The string read is: " + sentence);
        System.out.println("Length in chars is: " + sentence.length());
        System.out.println("All lowercase is: " + sentence.toLowerCase());
        System.out.println("All uppercase is is: " + sentence.toUpperCase());

        //store java index pos in variable
        java_index_number = sentence.indexOf("java");
        System.out.println("Found 'java' or at pos: " + java_index_number);


        //make first letter of java a capital letter
        java_capital_first_letter = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
                java_index_number + 1).toUpperCase() + sentence.substring(java_index_number + 1, java_index_number + 4)
                + sentence.substring(java_index_number + 4);

        //make java all caps
        java_all_caps = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
                java_index_number + 4).toUpperCase() + sentence.substring(java_index_number + 4);

        //output
        System.out.println("Changing to 'Java': " + java_capital_first_letter);
        System.out.println("Changing to 'JAVA': " + java_all_caps);

        // Keep console window alive until 'enter' pressed
        System.out.println();
        System.out.println("Done - press enter key to end program");
        stop_program = user_input.nextLine();


    }
}

How do I get the while loop to work?

UPDATE: after the awesome feedback that I got, I finally got it to work.. Thanks everyone who helped!

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //declare all variables
        String sentence;
        int java_index_number;
        String stop_program;
        String java_capital_first_letter;
        String java_all_caps;

        //scanner to get user input
        Scanner user_input = new Scanner(System.in);

        //prompt the user for a sentence
        System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
        sentence = user_input.nextLine();


        while(!sentence.toLowerCase().contains("java")) {
            System.out.println("Your sentence does not have the word 'java' within it.");
            System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
            sentence = user_input.nextLine();
        }

        //output
        System.out.println();
        System.out.println("The string read is: " + sentence);
        System.out.println("Length in chars is: " + sentence.length());
        System.out.println("All lowercase is: " + sentence.toLowerCase());
        System.out.println("All uppercase is is: " + sentence.toUpperCase());

        //store java index pos in variable
        sentence = sentence.toLowerCase();
        java_index_number = sentence.indexOf("java");
        System.out.println("Found 'java' or at pos: " + java_index_number);


        //make first letter of java a capital letter
        java_capital_first_letter = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
                java_index_number + 1).toUpperCase() + sentence.substring(java_index_number + 1, java_index_number + 4)
                + sentence.substring(java_index_number + 4);

        //make java all caps
        java_all_caps = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
                java_index_number + 4).toUpperCase() + sentence.substring(java_index_number + 4);

        //output
        System.out.println("Changing to 'Java': " + java_capital_first_letter);
        System.out.println("Changing to 'JAVA': " + java_all_caps);

        // Keep console window alive until 'enter' pressed
        System.out.println();
        System.out.println("Done - press enter key to end program");
        stop_program = user_input.nextLine();


    }
}

Upvotes: 1

Views: 3441

Answers (2)

Jacobo de la Rosa
Jacobo de la Rosa

Reputation: 91

Try to avoid all those different variables for "Java". I think you're trying to find when the java appear. So apply to your sentence an sentence.toLowerCase().contains("java") The user input will be converted to lower case and just check if contains the word java in lower case, so you can avoid the use of so many or. Inside your while just put a while(true), and check with an if(sentence.toLowerCase().contains("java")){break;}.

Upvotes: 1

Discoverer98
Discoverer98

Reputation: 461

The problem is in how you created the boolean clause inside the while. So, currently you have this:

while(!sentence.contains(java) || !sentence.contains(Java) || !sentence.contains(JAVA))

Let's suppose that the sentence contains "java". So, !sentence.contains(java) is false. However, !sentence.contains(Java) is true, since the sentence contains "java" but not "Java". Because you're using logical ORs (||) a single true is enough to make the entire clause true, and the inside of the while loop is executed.

Probably, what you're trying to do would be done this way:

while(!sentence.contains(java) && !sentence.contains(Java) && !sentence.contains(JAVA))

In that case, all of the clauses above have to be true, meaning that sentence cannot contain "java", "Java" or "JAVA".

Upvotes: 4

Related Questions