This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Word Search Project Help  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project where I have have to find specific words within a grid created by the 2D array. The user inputs the rows and columns.We have been given a class can WordBoard that has the following methods:
-public char[][] getBoard()
Gets the a copy of the 2D character array that WordBoard generated. This is the array that you
will be iterating over when you perform your word search.
-public String[] getDictionary()
Gets the list of words that may be hidden in the word board. Your code for finding a single word
inside the array should be surrounded by a loop that iterates over this array
-public void highlightWord(int i1, int j1, int i2, int j2)
This method should be called when you know where a word begins and ends. You’ll be passing
two ordered pairs (i1, j1) and (i2, j2) to the method. The first ordered pair should be the location
of the first character of the word, and the second one should be the location of the last character
of the word. After you have called this method, using the toString() method will capitalize
all of the words you’ve found

The following code is for finding the word if it goes from left to right, but it is not working. I don't know why...


 
Rancher
Posts: 624
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a fun assignment... School? Your array is an charArray?
Could we see the getBoard message/ rest of the code?
Do you have a list of words you have to search for?
It seems me hard to do if you don't...
For beginning java putting some random char together hoping to find a word seems excessive
Nevermind, since there is a getDictonary method, you also have something to compare to.
 
Rancher
Posts: 3352
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it is not working.


Can you explain what it does and why you think it is not working?
Copy and paste here any output that shows the problem.  Be sure to add some comments saying what is wrong with the output.
 
Manasa Sast
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The words can be diagonal, but writing the code to find them is not required for the project.
It is supposed to capitalize the words that have been found in the grid and print this if it was working correctly:


This is my output:


The output shows that none of the words have been capitalized, so none of them have been found by my code.

 
Norm Radder
Rancher
Posts: 3352
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A suggestion to make the code more readable: Replace the variable names i,j,k with more meaningful names: wordIndex, row and column
 
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you allowed to use String methods? For instance, you can turn all the rows and columns into Strings and simply use a suitable method to see if a word is contained in one of the Strings. That prevents having to do all that nasty index juggling.
 
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Are you allowed to use String methods? . . .

I would have thought not, from what it says in the first post. This is obviously an exercise in writing loops, which is why you end up with such awkward implementation details.

And, welcome to the Ranch MS
 
Sheriff
Posts: 12749
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Piet Souris wrote:Are you allowed to use String methods? . . .

I would have thought not, from what it says in the first post. This is obviously an exercise in writing loops, which is why you end up with such awkward implementation details.


Which is why, in my opinion, it's a suboptimal way to teach the concept of loops. I think the teacher could have done better. I would have liked to see something like the following in the instructions:

You will have to break this problem down into smaller, more manageable pieces. That is, instead of trying to find every word in all directions, in all possible rows/columns/diagonals, first try to think of how you would find one word in one row in one direction. Once you have something that does that, then you might try to find one word in one row going in the other direction. Then just keep stepping in more of the problem. When you know how to find words in a row, try to expand that to searching all rows. Then try to do the same kind of things for columns, then for diagonals. Once you solve a small part of the problem, you will most likely find that you can use the same logic to solve other similar problems. For example, a method like the one outlined below can be used to locate a word in a sequence of characters. That sequence of characters can be from a row, column, or diagonal.

Maybe even start them off with a simpler problem like this:

Implement the locateWord() method below so that it does what the JavaDoc comments describe:

Your solution should use the constants START and ORIENTATION to make the code more readable and avoid the use of magic numbers.

Even this problem can be broken down into smaller, simpler chunks.
 
Junilu Lacar
Sheriff
Posts: 12749
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Manasa Sast wrote:The following code is for finding the word if it goes from left to right, but it is not working. I don't know why...


 It's really not surprising that you wouldn't know why it's not working. In fact, when I look at code like that, I am instantly suspicious of whether it works at all. That code is way too complex. Simplify it. Don't try to solve everything all at one go: break the big problem down into smaller problems.

 
Carey Brown
Saloon Keeper
Posts: 5168
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Manasa Sast wrote:


Your comments are more on track than your code. You need to expand this into pseudo code and then look at implementing each step in its own method. Example pseudo code might go something like this
 
Junilu Lacar
Sheriff
Posts: 12749
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Return true or false in that innermost part of the pseudocode is probably not the best way to say that you want to go on to the next iteration of the previous level enclosing loop (for each direction)
 
Carey Brown
Saloon Keeper
Posts: 5168
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm open to suggestions.

(Don't know if we've lost the OP or not.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!