if statement - Profanity filter- Java -
we have filter out 3 words cat, dog, , llama. program has filter out these words when varying in case cat.
import java.util.scanner; public class assign5 { public static void main(string[] args) { string cat,dog,llama,x,y,z; system.out.println("enter word"); scanner keyboard = new scanner (system.in); x=keyboard.next(); y=x.tolowercase(); if (y.indexof("cat")!=-1||y.indexof("dog")!=-1|y.indexof("llama")!=-1) { system.out.println("profanity detected"); } else if(y.indexof("cat")!=-1||y.indexof("dog")!=-1|y.indexof("llama")!=-1) { y.charat(0); y.charat(1); y.charat(2); y.charat(3); system.out.println("no profanity detected"); } } }
output:
enter word
dogmatic
profanity detected
i trying weed out words contain 3 words. it's not reading it. tried splitting characters using charat isn't working. keeps reading if. how make case sensitive? detect "cat" profane considers "cat" profane well. can't use: array, , while or split. right now, can work if-else, switch, , charat split.
@ scrum meister: posted 1 well. repost since still having trouble working thing.
to point in right direction, i'll point string documentation. namely, equals function. on stackoverflow try not give exact examples homework, guide student in right direction. if have specific questions how works or similar, we'll glad help.
one piece of advice, don't want call y=x.tolowercase();
because once you've done that, have no way differentiate between "cat" , "cat" (since both "cat").
Comments
Post a Comment