Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i break the String to bits?

 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what i am trying to do here, is to make it possible for the user to type 2 lines, and then say(or display) whether the string of the first sentence correspond to the string of the second sentence.

so for example:

first sentence:
i will go shopping

second sentence:
Tomorrow, i will go swiming.

" i will go" corresponds in the second sentence. so how can i make the program look for corresponding words, and identify them?

so far , i have written this. but i dont know how to finish.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dmitri Makovetskiy wrote: ... " i will go" corresponds in the second sentence. so how can i make the program look for corresponding words, and identify them? ...

If you're looking for corresponding words, then I would not bother with individual chars. I would probably use String's split method to break the input into words, then write a method to compare sequences of these words using String's equals (or equalsIgnoreCase) method. Do not use ==.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do it by character by character, then you'll end up with meshed code. Because, for example, in your first sentence, there is a char i, so you search for i in the second sentence, It's OK. But how do you put a space between the next word? And, for the second character w, there is a w in your first sentence's word Tomorrow. then?.....

It's better to go for checking word by word,,,,,
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
Dmitri Makovetskiy wrote: ... " i will go" corresponds in the second sentence. so how can i make the program look for corresponding words, and identify them? ...

If you're looking for corresponding words, then I would not bother with individual chars. I would probably use String's split method to break the input into words, then write a method to compare sequences of these words using String's equals (or equalsIgnoreCase) method. Do not use ==.



the problem comes when the user might write a sentence with 100 ways. i cant compare all his words using loops or for methods.


i thought to make two variables

string end= " "

String Word= end+Str.charAt() + end

but how can i number each variable and make it look at the second string with no problem




i also came up with this one. but it tells me that St3, failed to initialize. how do i fix it and why is it caused?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dmitri Makovetskiy wrote:...the problem comes when the user might write a sentence with 100 ways. i cant compare all his words using loops or for methods...

I think you can. But you need to be very careful about defining your algorithm -- the logical process you will use. Try writing this out step by step in Engligh before trying to code it.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if " " character, i cant tell java to stop counting words before that space, can i ?


is there a way i could make java count all characters till " ", and then add all that character string to a variable called Word. then make java pass onto another String (after " ") find its characters till it reaches " ",
and add it to a word?
another variable
Words=Word+Words


is this direction possible? can it be done?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic