• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting possible words from large data using java

 
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have dictionary data in the form of array of string. This application get characters input from user and print all the possible words from dictionary data based on user input.

I wrote this code, which works fine but is there any other way, I can impove the my code when using large amount of data. For example, Shall I use Collections to store the dictionary data and use binarysearch method or any other method to to acheive it.



In this example I just assumed, I have dictionary data of three characters and user input is also characters.


Thanks.


Kind regards,
Matloob
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matloob Hussain wrote:is there any other way, I can impove the my code when using large amount of data. For example, Shall I use Collections to store the dictionary data and use binarysearch method or any other method to to acheive it.


Collections are certainly worth knowing about - especially for the future - and one you might want to look at now is a Set ( java.util.Set ).

Another is sorting your dictionary; that allows you to make use of searches that work much more quickly - although based on what your code is currently doing you probably don't need it.

Also, don't use constructs like ""+input[y] if you can possibly help it; they are clumsy and very slow - and in this case there's a perfectly good alternative.
Look at the java.lang.String class and I'm sure you'll find it.

Another thing: Try and make your methods do ONE THING. This one, for example:does two. It finds the word AND updates the result, and it doesn't need to.

Methods should normally return a value. Have a think how you might return one that allows the method to do exactly what its name says (and NOTHING ELSE).

HIH

Winston

PS: I broke up that very long line of yours. Please re-read the UseCodeTags page.
 
Matloob Hussain
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,

Thanks for your reply.

As you advised to write separate function for each thing and also java.lang.String instead of ""+input[y]. Here is the updated code, Please let me know, if is it correct?


One thing more, which one shall I use java.util.Set or binary search. I am allowed to change the algorithm of my code. The task is to find all possible words from dictionary data of given characters. If i use binarysearch() method and finds all possible word which contains all these given characters.

Thanks in advance


Matloob
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matloob Hussain wrote:Thanks for your reply...


You obviously didn't read all of it: Please don't put very long lines inside code blocks (in this case, your dictionary defintiion line). I've broken it up AGAIN.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matloob Hussain wrote:HPlease let me know, if is it correct?


Better, but you've used a Character method rather than looking at String.
(Another hint: there is a method in the String class that does exactly what you want AND takes a char).

Second: What does the code inside your checkWord() loop do exactly?
Don't tell me:
1. STOP programming. In fact, I'd advise you to turn your computer OFF.
2. Get out a pencil and paper and go through a few scenarios, working out precisely what your loop does on each iteration.

Simple fact is: it's wrong; but you ain't going to learn why if I simply tell you.

PS: You might want to look again at what break does (although that's not the basic problem).

Winston
 
Matloob Hussain
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,

Thank you for your reply.

First One: I used indexOf method of String as:


please let know. Is it correct?

Second One: I used break to stop the for loop. If any character entered by user as an input.

let suppose , I have array {"tea", "bat", "eat"} and user input :'a' , 'e', 't'

my first loop pick the first element which is "tea" and then second loop runs and check all three characters which this string contains.
When it picks second element which is "bat" and first character 'b' does exists in "bat" string then break keyword works and stop the second loop and then check for third element which it contains three character so output will be "tea" and "ate".

Please advise, thanks...

I need to submit this code today. Please give me all the information which you can?

Thanks once again.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matloob Hussain wrote:First One: I used indexOf method of String as:please let know. Is it correct?


Yes, although:
word.indexOf(input[y]) >= 0
is more usual.

BTW, try and space your code out a bit; it makes it much easier to read. The compiler doesn't worry about a few extra spaces.

Second One: I used break to stop the for loop.


Good. It's usually simplest (although you don't want to overuse them).

If any character entered by user as an input....


I have to admit, I'm a bit mystified now. What exactly is it you want your code to do?

What you seem to be describing now is something that only returns words that match ALL characters.
And if that's the case, what do you do about repeated characters, eg:
"deer" ← {'d', 'e', 'r')

You really do need to TellTheDetails. (←click).

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic