java - while loop: while sentence does not contain a word -


i'm getting user input , checking see if word 'java' in sentence. did while loop when word 'java' in sentence, tells me it's not , continues while loop. if remove while loop, else want program works. here code:

import java.util.scanner;      public class main {      public static void main(string[] args) {          //declare 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 user input         scanner user_input = new scanner(system.in);          //prompt user sentence         system.out.print("enter line of text containing 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 not have word 'java' within it.");             system.out.print("enter line of text containing 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: " + sentence.touppercase());          //store java index pos in variable         java_index_number = sentence.indexof("java");         system.out.println("found 'java' or @ pos: " + java_index_number);           //make first letter of java 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 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 'java': " + java_capital_first_letter);         system.out.println("changing 'java': " + java_all_caps);          // keep console window alive until 'enter' pressed         system.out.println();         system.out.println("done - press enter key end program");         stop_program = user_input.nextline();       } } 

how while loop work?

update: after awesome feedback got, got work.. helped!

import java.util.scanner;  public class main {      public static void main(string[] args) {          //declare variables         string sentence;         int java_index_number;         string stop_program;         string java_capital_first_letter;         string java_all_caps;          //scanner user input         scanner user_input = new scanner(system.in);          //prompt user sentence         system.out.print("enter line of text containing word 'java' somewhere within it: ");         sentence = user_input.nextline();           while(!sentence.tolowercase().contains("java")) {             system.out.println("your sentence not have word 'java' within it.");             system.out.print("enter line of text containing 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: " + sentence.touppercase());          //store java index pos in variable         sentence = sentence.tolowercase();         java_index_number = sentence.indexof("java");         system.out.println("found 'java' or @ pos: " + java_index_number);           //make first letter of java 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 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 'java': " + java_capital_first_letter);         system.out.println("changing 'java': " + java_all_caps);          // keep console window alive until 'enter' pressed         system.out.println();         system.out.println("done - press enter key end program");         stop_program = user_input.nextline();       } } 

try avoid different variables "java". think you're trying find when java appear. apply sentence sentence.tolowercase().contains("java") user input converted lower case , check if contains word java in lower case, can avoid use of many or. inside while put while(true), , check if(sentence.tolowercase().contains("java")){break;}.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -