Monday 15 August 2011

java - cannot invoke equals(string) to a primitive type char -



java - cannot invoke equals(string) to a primitive type char -

why getting error cannot invoke equals(string) primitive type char here. below can see total code. please help me kinda stuck here dont know how alter primitive stuff , is? ideas.

if (dolzina == 3) { for(i=0 ; < user_input.length(); i++){ if (user_input.charat(i).equals("*")); // error i--; break; } prefix=user_input.substring(0,i); system.out.println(prefix); }

this total code can see. reads txt , save words lenght 3 array. want @ moment when user inputs a** ecample programme binary search in array looking fist word starting , lastly word , searching possible words. getting error. please help.

public class proba { private final static int not_found = -1; public static void main(string[] args) { string izbira; int dolzina=0; int i; scanner in = new scanner(system.in); string user_input; scanner input = new scanner(system.in); string regex; list<string> list3 = new arraylist<string>(); int beseda; string prefix; seek { file file = new file("sort.txt"); filereader filereader = new filereader(file); bufferedreader bufferedreader = new bufferedreader(filereader); string vrstica; while ((vrstica = bufferedreader.readline()) != null) { if (vrstica.length() == 3) { list3.add(vrstica); } } system.out.println(list3); do{ { system.out.println("enter lenght of word:"); if (in.hasnextint()) { dolzina = in.nextint(); } else if (in.hasnextline()) { system.out.printf("wrong entry!%n ", in.nextline()); } } while (dolzina <= 0); collections.sort(list3); system.out.println("enter word unknown character come in * :"); user_input = input.nextline(); user_input = user_input.replace("*", "."); system.out.println("sorted list: [length: " + list3.size() + "]"); if (dolzina == 3) { for(i=0 ; < user_input.length(); i++){ if (user_input.charat(i).equals("*")); // error i--; break; } prefix=user_input.substring(0,i); system.out.println(prefix); } dolzina=-1; system.out.println("ponovni vnos (da/ne):"); scanner inn= new scanner (system.in); izbira = inn.next(); }while (izbira.equalsignorecase("da")); bufferedreader.close(); } grab (ioexception e) { e.printstacktrace(); }} }

primitive types don't have methods, can't invoke equals on them. primitive types, should utilize == operator:

user_input.charat(i) == '*'

you have utilize single quotes '*' interpreted char. if used double quotes, string (one happens have 1 character), , can't compare char , string using ==. same as:

char c = user_input.charat(i); string s = "*"; boolean areequal = (c == s); // error: incomparable types: char , string

java

No comments:

Post a Comment