Java: BufferedReader lines compared to String issues -
i have program reading text file has list of items, creates arraylist consisting of items reads, compares few chosen words. example, text file contains (without numbering):
- book
- desk
- food
- phone
- suit
and reads each 1 , adds arraylist. when tried comparing string s = "book" each element in arraylist, find s not equal anything. have in method:
for (int = 0; < list.size(); i++) { if (s.equals(list.get(i)) return true; }
s.contains() doesn't work either. when print arraylist, there's additional whitespace @ end of each string element. how can comparison work when s = "book"? why there additional whitespace?
use trim() remove leading , trailing whitespace.
if (s.equals(list.get(i).trim()) return true; }
instead of
if (s.equals(list.get(i)) return true; }
Comments
Post a Comment