java - Logical issue with loops -
i'm making programm takes sentence input, creates array of these words , displays wether word redundant or isn't.
if "hello hi hello" scanned, program should advert user there's redundancy.
public static void main(string[] args) { scanner sc = new scanner(system.in); string sentence ; system.out.println("enter sentence :"); sentence = sc.nextline(); string[] t = sentence.split(" "); //split sentence @ each " " array int i=0, o=0 ; //iterators boolean b=false; //redundancy condition for(string s : t) // each string of t { system.out.println("t["+i+"] = "+ s); while(b) //while there's no redundancy { if(o!=i) //makes sure strings not @ same index. { if(s==t[o]) { b=true; //redundancy true, while stops } } o++; } i+=1; } if(b) { system.out.println("there identical words."); } else { system.out.println("there no identical words."); } }
here working code -
while(o<t.length && !b) { if(o!=i) { if(s.equals(t[o])) { b=true; } } o++; } i+=1; }
Comments
Post a Comment