java - if contains two words -


i brand new @ coding , can not application run right. please help!

i have written following code hw assignment:

import java.util.scanner;  public class hw1q2 {     public static void main(string[] args)     {         scanner keyboard = new scanner(system.in);         string sentence, str1, str2;          system.out.println("enter sentence containing either word \"blue\" or word \"green\" both or neither");         sentence = keyboard.nextline();         str1 = "blue";         str2 = "green";          if(sentence.contains("blue")); if(sentence.contains("green")){             system.out.println("sunny");}         else{         if(sentence.contains("blue")){             system.out.println("ocean");}         else{         if(sentence.contains("green")){             system.out.println("garden");}         else{             system.out.println("dull");         }}}     } } 

the goal return

  • garden if type green
  • ocean if type blue
  • sunny if type both and
  • dull if type neither

the problem if write sentence includes green, still returns sunny not garden.

you'll need use && , check blue , green. logic below should work. order critical. have check both words before start checking either word. otherwise print out ocean or garden before can print sunny.

    if(sentence.contains("blue") && sentence.contains("green")) {         system.out.println("sunny");     } else if (sequence.contains("blue")) {         system.out.println("ocean");     } else if (sequence.contains("green")) {             system.out.println("garden");     } else {         system.out.println("dull");     } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -