Another .equals question please!

Ok guys sorry, but i'm back.

Is there anyway you can take single terms from a .equals statment for it to be true.

For example if the user imputs "I have a Headache".

but my code line says.

if(symptom.equals("headache"));

Is there anyway for that to execute through some method which grabs key terms?

Ty

MS

Message was edited by:

Progr@mer

[402 byte] By [Progr@mera] at [2007-11-26 12:21:04]
# 1
if (symptom.toLowerCase().indexOf("headache") >= 0) ...Read the API. http://java.sun.com/j2se/1.5.0/docs/api/index.html
warnerjaa at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 2

That was quick!

Not sure I completely understand your question but try something like this:

if(hasSymptom(symptom))

{

//...

}

Make your method like this:

private boolean hasSymptom(String s)

{

//loop through an array of Strings or something like that

//return true if s equals an element in that array

//return false otherwise

}

Make sense?

CaptainMorgan08a at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 3

> Ok guys sorry, but i'm back.

> Is there anyway you can take single terms from a

> .equals statment for it to be true.

>

> For example if the user imputs "I have a Headache".

> but my code line says.

>if(symptom.equals("headache"));

> nyway for that to execute through some method which

> grabs key terms?

>

if using 1.5.0 look at the string api for the term contains.

otherwise look at using indexOf

Aknibbsa at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 4

> That was quick!

>

> Not sure I completely understand your question but

> try something like this:

> [code]if(hasSymptom(symptom))

> {

>//...

> code]

>

> Make your method like this:

> [code]

> private boolean hasSymptom(String s)

> {

> //loop through an array of Strings or something

> like that

> //return true if s equals an element in that

> array

>//return false otherwise

> ode]

>

> Make sense?

Sorry if im gonna sound ignorant but i don't know what private does.

Does it find the key term in the users input and make it true through my loop if it .equals something?

Maybe i should stop asking questions over my head huh?!

Ty

MS

Progr@mera at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 5
> Maybe i should stop asking questions over my head> huh?!Certainly until you have been through the tutorial.
sabre150a at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 6

> Sorry if im gonna sound ignorant but i don't know

> what private does.

> Does it find the key term in the users input and make

> it true through my loop if it .equals something?

I think I misunderstood your problem. Read warnerja's or Aknibb's response.

CaptainMorgan08a at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 7

I guess you may want to use the following method:

public boolean regionMatches(boolean ignoreCase,

int toffset,

String other,

int ooffset,

int len)

Tests if two string regions are equal.

A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len.

In case you need more, you should write a method of your own to extract string in your own way. It's usually not something hard to do.

principlesa at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 8

If your all confused about my question like me :) let me post it then say what i want it to do and how i can do that.

Here it is and pay attention to the if statement which has the .equals in it.

import java.util.*;

import java.text.*;

public class Medicine

{

public static void main(String[]args)

{

String symptom, answer;

Scanner scan=new Scanner(System.in);

do{

System.out.println("Please enter the symptom(s) you are experiencing:");

symptom=scan.nextLine();

System.out.println();

System.out.println();

if(symptom.equalsIgnoreCase("I have a headache") ||(symptom.equalsIgnoreCase("My head hurts" )|| symptom.equalsIgnoreCase("headache")))

{

System.out.println("You need tylenol or asprin");

}

else if(symptom.equalsIgnoreCase("I have trouble sleeping at night"))

{

System.out.println("You need Niquill

else

System.out.println("I'm sorry i did not find any medicine which will meed your symptom");

System.out.println("Would you like to try again?");

answer=scan.nextLine();

}while(answer.equalsIgnoreCase("y"));

}

}

Ok when the user imputs can't sleep it will go onto the else statement and say "sorry i did not find the medicine". right?

But what i want it to do is execute the second else if because it has the word sleep in there.

I hope thats better.

Progr@mera at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 9
Read reply 1 and 3. You might want to use the toLowerCase() or toUpperCase() methods to ignore the case.
CaptainMorgan08a at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 10

> Read reply 1 and 3. You might want to use the

> toLowerCase() or toUpperCase() methods to ignore the

> case.

I hear you on that, but wouldn't that just change the cases of the words?

It will still send me to the else statment right?

If i enter can't sleep as a user.

I want it to print out you need niquill for example.

But in the program i would have to .equal the input to the string right?

merely changing the case from upper to lower would do it

?

Progr@mera at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 11
Use the contains() method and convert both Strings to upper case.
CaptainMorgan08a at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 12

What we have said can tell you if a word is in a string, but for what you want (If I understand what you are trying to do) you will either need to do something else or build a full list of all the possible descriptions of the symptoms.

I am guessing that you want to be able to have the tylenol or asprin answer come up if they say

"headache" or "head" or "brain hurts" or anything long those lines what we have said will require a full list of all possible symptom descriptions.

Aknibbsa at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 13

> Use the contains() method and convert both Strings to

> upper case.

Ok bro you already know how noobish i am at this any visual example of how to do that would help please, for example something like this?

[code]

if(symptom.UpperCase("has a headache"));

?

im lost

Progr@mera at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 14

> What we have said can tell you if a word is in a

> string, but for what you want (If I understand what

> you are trying to do) you will either need to do

> something else or build a full list of all the

> possible descriptions of the symptoms.

>

> I am guessing that you want to be able to have the

> tylenol or asprin answer come up if they say

> "headache" or "head" or "brain hurts" or anything

> long those lines what we have said will require a

> full list of all possible symptom descriptions.

BINGOOOOOO!!!!!

wow your good at communication and programming.

Any solutions on how to do what you just said?

Progr@mera at 2007-7-7 15:12:05 > top of Java-index,Archived Forums,Socket Programming...
# 15
Nyquil should not be suggested for people who can't sleep. You should suggest Unisom or Sominex or something like that if insomnia is their only problem. Nyquil is for when you can't sleep because you have a cold with cough, runny nose, etc. ;-)
doremifasollatidoa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 16

> if(symptom.UpperCase("has a headache"));

if(symptom.contains("headache");

That will check if the word "headache" appears anywhere in that String. If you want it to be case-insensitive, convert both of them to lower case or upper case. For more information, read through the [url http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html]String[/url] page.

CaptainMorgan08a at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 17

> Nyquil should not be suggested for people who can't

> sleep. You should suggest Unisom or Sominex or

> something like that. Nyquil is for when you have a

> cold with cough, runny nose, etc. ;-)

>

> Answer 1 should answer your real question, though.

LOL i know i was just experminting it not publishing it yet :).

Now we all know your the doctor in the house at the moment nyquil is the only thing that came to mind when thinking about not sleeping.

Imagine if a kid is unable to sleep because they are having nightmares, my program wouldhv'e prescribed her nyquill ^^.

Any real solutions to my real problem doctor?

Progr@mera at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 18

you want something like this:

String headsymptoms = "headache, head, sore brain";

if(headsymptom.contains("input")

{

//print out the nyquil

}

What I would highly reccommend instead is giving them a list of choices as otherwise you will have a very hard time getting this to work.

Aknibbsa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 19
> BINGOOOOOO!!!!!> wow your good at communication and programming.> Any solutions on how to do what you just said?Ok, now Im confused again...
CaptainMorgan08a at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 20

> BINGOOOOOO!!!!!

> wow your good at communication and programming.

> Any solutions on how to do what you just said?

Set headacheSet = new HashSet();

headacheSet.put("headache");

headacheSet.put("head hurts");

...

Iterate over the headacheSet (include toUpperCase or toLowerCase on both symptom and the value coming out of the Set):

if (symptom.contains((String)headacheSetIter.next()))

// recommend tylenol/aspirin/advil

if (symptom.contains((String)insomniaSetIter.next()))

// recommend Unisom

doremifasollatidoa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 21

> you want something like this:

>

> String headsymptoms = "headache, head, sore brain";

>

> if(headsymptom.contains("input")

> {

>//print out the nyquil

>

> What I would highly reccommend instead is giving them

> a list of choices as otherwise you will have a very

> hard time getting this to work.

Oooooooooooooh, I think I get it now. So a combination of replies 1, 2, and 3 should do the trick.

CaptainMorgan08a at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 22

> > What we have said can tell you if a word is in a

> > string, but for what you want (If I understand

> what

> > you are trying to do) you will either need to do

> > something else or build a full list of all the

> > possible descriptions of the symptoms.

> >

> > I am guessing that you want to be able to have the

> > tylenol or asprin answer come up if they say

> > "headache" or "head" or "brain hurts" or anything

> > long those lines what we have said will require a

> > full list of all possible symptom descriptions.

>

>

> BINGOOOOOO!!!!!

> wow your good at communication and programming.

> Any solutions on how to do what you just said?

If I were u, I would break the code into several functions:

private boolean isAspirinWork(String symtom) {

}

private boolean isTylenolWork(String symtom) {

}

//main code:

if (isAspirinWork(symtom)) {

//do aspirin here

}

else if (isTylenolWork(symtom)) {

//do tylenol here

}

else {

//do whatever you want here

}

principlesa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 23

What he wants to be able to do is have the user put in their description and if it sounds like it's a headache then prescribe an asprin. The problem with this is that there are virtually unlimited possibilities that the user will put in.

The real solution to this problem is to limit the problem scope so that the user picks from a list of symptoms rather than having them describe their symptoms

Aknibbsa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 24

> you want something like this:

>

> String headsymptoms = "headache, head, sore brain";

>

> if(headsymptom.contains("input")

> {

>//print out the nyquil

>

> What I would highly reccommend instead is giving them

> a list of choices as otherwise you will have a very

> hard time getting this to work.

Makes sense.

So maybe i should do Switch statements.

But if i do case then i would have to comeback with some more questions.

LOLL im kidding, thanks for all the help guys let me try out all of those possibilities.

Progr@mera at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 25

So something like this should work:

private boolean isHeadacheSyptom(String symptom)

{

String possibleSymptoms = "headache, sore head, other crap";

return possibleSymptoms.contains(symptom.toLowerCase());

}

CaptainMorgan08a at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 26

> LOL i know i was just experminting it not publishing it yet :).

> Now we all know your the doctor in the house at the

> moment nyquil is the only thing that came to mind

> when thinking about not sleeping.

My husband is sick (almost better), and has been taking Nyquil. So, it was on my mind. :)

> Imagine if a kid is unable to sleep because they are

> having nightmares, my program wouldhv'e prescribed

> her nyquill ^^.

Send her to her parents' bed. :)

> Any real solutions to my real problem doctor?

See my post about Sets.

doremifasollatidoa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 27

> > you want something like this:

> >

> > String headsymptoms = "headache, head, sore

> brain";

> >

> > if(headsymptom.contains("input")

> > {

> >//print out the nyquil

> >

> > What I would highly reccommend instead is giving

> them

> > a list of choices as otherwise you will have a

> very

> > hard time getting this to work.

>

>

> Makes sense.

> So maybe i should do Switch statements.

>

> But if i do case then i would have to comeback with

> some more questions.

>

> LOLL im kidding, thanks for all the help guys let me

> try out all of those possibilities.

Better off to come back with questions to use a proper solution instead of using what you know and making it a real kludge.

Aknibbsa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 28

> > LOL i know i was just experminting it not

> publishing it yet :).

> > Now we all know your the doctor in the house at

> the

> > moment nyquil is the only thing that came to mind

> > when thinking about not sleeping.

>

> My husband is sick (almost better), and has been

> taking Nyquil. So, it was on my mind. :)

>

> Imagine if a kid is unable to sleep because they

> are

> having nightmares, my program wouldhv'e prescribed

> her nyquill ^^.

>

> Send her to her parents' bed. :)

>

> > Any real solutions to my real problem doctor?

>

> See my post about Sets.

Sets only works if you can make a big enough set to cover most of the possible answers - but for that you have to know how the target group thinks.

Aknibbsa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 29

> What he wants to be able to do is have the user put

> in their description and if it sounds like it's a

> headache then prescribe an asprin. The problem with

> this is that there are virtually unlimited

> possibilities that the user will put in.

>

> The real solution to this problem is to limit the

> problem scope so that the user picks from a list of

> symptoms rather than having them describe their

> symptoms

Agree. Machines are not human being and in order to make them work, he will need to list all the symtoms. No other ways to do that. However, he could make the string-extracting, processing as complicate as he wants.

principlesa at 2007-7-7 15:12:08 > top of Java-index,Archived Forums,Socket Programming...
# 30
I will make sure to post my work so you guys can feel proud of me ^^.Give me about 5 minutes should have a sample in for you guys.
Progr@mera at 2007-7-7 15:12:10 > top of Java-index,Archived Forums,Socket Programming...
# 31

System.out.println("Enter symptom");

input: I have a headache.

If(symptom.equals(headache));

{

System.out.println("You need to stay away from Programmer he asks too many questions");

System.out.println("Solution is to kik him out ^^ :)");

}

LOl

Progr@mera at 2007-7-7 15:12:10 > top of Java-index,Archived Forums,Socket Programming...
# 32
I count three errors in your code. ;-)(All on the same line)
CaptainMorgan08a at 2007-7-7 15:12:10 > top of Java-index,Archived Forums,Socket Programming...
# 33
> I count three errors in your code. ;-)> (All on the same line)I know i did it fast for you guys, aren't you proud:)!
Progr@mera at 2007-7-7 15:12:10 > top of Java-index,Archived Forums,Socket Programming...