0

I'm Trying To Make A Chat Bot But It Always Comes Up With A Error When I Type:

if(Write == "hi"){
    Reply.setText("HI!");
}

It Will Come With The Error: jtextarea incomparable with String

What Shall I Do?

Barkermn01
  • 6,781
  • 33
  • 83
Ots Wng
  • 3
  • 6

2 Answers2

0

Not much context in the question to work with, but I suppose you are looking for

if ("hi".equals(Write.getText()))

By the way, never compare strings with == unless you really want them to be exactly the same instance of the String class.

Damiano
  • 791
  • 1
  • 8
  • 22
0

Explanation

You can not compare completely different objects with each other by using ==. Write is of type JTextArea and "hi" is of type String. Those objects have nothing in common thus the compiler complaints.


Solution

You probably wanted to compare the text stored inside JTextArea with the text "hi". You access this text by using the JTextArea#getText method (documentation).

Now note that you never (except you know what you do) should compare Strings by using ==. The result will not be what you expected. Use String#equals instead. Here's more on this topic: How do I compare strings in Java?

So your code should probably look like:

if ("hi".equals(Write.getText())) {
    Reply.setText("HI!");
}

Naming conventions

Last note that you should stick to naming conventions. Variable names, also method names, should always begin with a lowercase character. Uppercase is only used for class names (and constants). So you should rather write write and reply:

if ("hi".equals(write.getText())) {
    reply.setText("HI!");
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • sorry, but coz im a nooby i probally wont understand all of this – Ots Wng Jan 06 '18 at 18:02
  • @OtsWng That is no problem, just tell me what exactly confuses you and I will explain it in more detail. – Zabuzard Jan 06 '18 at 18:06
  • th.e guy at the top already explained it to me but he didn't explain what i really needed, because i needed something more easier for me to type it. like for example if i typed hi, it would randomize all the reply statements but i didn't know how to store all of it in one variable, and print it out. for example if i said hi, it would either say:hi,how r u, and so on. – Ots Wng Jan 06 '18 at 18:12
  • @OtsWng Mhh, your that does not seem to be related to your question. Please create a new question with all information necessary. You may then link the follow-up question here. – Zabuzard Jan 06 '18 at 18:15
  • i didn't now how to randomize them and i didn't know how to use:`Math.random` so i just scraped the whole chatbot idea because i didn't know much code that time. also i found out that i shouldn't use:`if ("hi".equals(write.getText())) {` because what if i said: hi :-] it wouldn't say hi so i should use: `if ("hi".contains(write.getText())) {` so when it contains it it would randomize then print. can u help me – Ots Wng Jan 06 '18 at 18:16
  • i'l, try but i ran out of questions untill tomorrow. i'll try! – Ots Wng Jan 06 '18 at 18:17