java - Issue while comparing a long string with a string of array -
main idea: have url (string) , have array few data's (string). want check whether strings inside of array found in url or not using contains()
method.
my code structure: first of string words stored inside text file. read file , store values inside jtextarea
. , jtextarea
use gettext()
method , storing values inside array. , now, check strings using contains
method. here code:
this function (working fine) reads text file , write inside jtextarea
.
jtextarea jta = new jtextarea(300,300); reader reader = null; try { reader = new filereader(new file("res/pass.txt")); jta.read(reader, "the force strong one"); } catch (exception exp) { exp.printstacktrace(); } { try { reader.close(); } catch (exception exp) {} }
wait before that, these words stored inside pass.txt
earlier. example:
red~
green~
yellow~
black~
this function (working fine) know length of array.
string getarr=""; int getcount=0, z=0, lens = jta.gettext().length(); for(int i=0; i<lens; i++){ if(jta.gettext().charat(i)=='~'){ getcount++; } }
this function (working fine) store strings inside array.
string[] arr = new string[getcount]; for(int i=0; i<lens; i++){ if(jta.gettext().charat(i)!='~'){ getarr = getarr+jta.gettext().charat(i); } else{ arr[z] = getarr; getarr=""; z++; } }
the problem starts here. tried print array values in console , it's displaying values. but, when comparison it's not working expected.
string txtgeturl = txturl.gettext(); //the url boolean ok=true; for(int i=0; i<arr.length; i++){ system.out.print(arr[i]); } for(int i=0; i<arr.length; i++){ if(txtgeturl.tolowercase().contains(arr[i].tolowercase())){ ok=false; } } if(ok==false){ joptionpane.showmessagedialog(null, "url blocked!"); } else{ joptionpane.showmessagedialog(null, "whitelist url"); }
let say, sample url enter in textfield http://www.example.com/ex/examplered.html
instead of displaying url blocked
it's displaying whitelist url
. please me solve problem. in advance.
when dealing string
objects, there no need hand build own parsing based on characters , indexes, there many methods available help.
here, you'll want use split()
, trim()
.
initially values red~
, put text area, add white space it. value deal be: red ~ \n
or variant.
so, first use split()
on ~
character , use trim()
on first element of resulting split array.
here i've written quick test class show in action:
public class blockedwords { public static void main(final string[] args) { //various different types of white space within input. final string[] input = {" ~\n\r","\n\rb ~","\ne\r~"," g\n~"}; final string[] urls = {"www.url.com/a","www.url.com/b","www.url.com/c","www.url.com/d", "www.url.com/e","www.url.com/f","www.url.com/g","www.url.com/h"}; final blockedwords whitelisting = new blockedwords(input); (final string url : urls) { if( whitelisting.containsblockedword(url) ) { system.out.println(url + " blocked."); continue; } system.out.println(url + " whitelisted."); } } private string[] blockedwords; public blockedwords(final string[] blockedwords) { (int = 0; < blockedwords.length; i++) { //split on ~ character array of {"word", ""} take word. in index 0. //then trim in case there whitespace characters still around word. blockedwords[i] = blockedwords[i].split("~")[0].trim(); } this.blockedwords = blockedwords; } public boolean containsblockedword(final string url) { (final string blockedword : this.blockedwords) { if( url.tolowercase().contains( blockedword.tolowercase() ) ) { return true; } } return false; } }
and output this:
www.url.com/a blocked.
www.url.com/b blocked.
www.url.com/c whitelisted.
www.url.com/d whitelisted.
www.url.com/e blocked.
www.url.com/f whitelisted.
www.url.com/g blocked.
www.url.com/h whitelisted.
Comments
Post a Comment