I would like to compare strings but the following code won’t work. I’m guessing it’s because incompatible, is there any other way to do it?Thanks!
public class test
{
public static void main(String[] args)
{
String str1 = "abc def ghi abc ded abc ghi";
String str2 = "ghi";
int count = 0;
String array[] = str1.split("\t");
for(int i = 0; i < array.length; i++)
{
if(array[i].equals(str2))
{
count++;
System.out.println(array[i]);
System.out.println(count);
}
}//for
}//main
}//test
It looks like you’re splitting on
"\t"(tab), but your string is" "(space) separated. Try Stringarray[] = str1.split(" ");instead.