• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Word Finder  RSS feed

 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, my deadline has been moved a week earlier. Meaning I have 9 hours left to figure out the last 2 exercises of my assignment. I could use any help please. Reading from file, storing to an array and printing it is just annoying af.
Exercise requirement:
This exercise involves writing a program to solve word search puzzles. Create a Java class WordFinder with code as below.


Then download two files containing the grid and the words to search for: grid.txt, words.txt
Part A
In class WordFinder, implement method initGrid. This method should store the contents of file grid.txt in the 2D char array grid declared as a static member of the class.

   The program should read, in turn, each line of the file and write it to the corresponding row of the array.
   You can assume that the file contains exactly 10 lines with 10 characters on each line.
   Grids will contain only lower-case letters.

Next implement method printGrid() which should display the grid on the screen. You may want to separate characters by a blank in order to improve legibility.

Run the program and check that it inputs and displays the grid correctly.

Part B
In class WordFinder, implement method find(). This method should read words from file words.txt. For each word, it should search the grid for a horizontal (left-to-right) or vertical (downwards) occurrence. If the word is found in the grid, then it should display the details of the occurrence in a format such as below:
grammar: row 8, position 1

   Please include the row and/or column number and the starting position of the occurrence. Numbering should start at zero as usual.
   There should be no output for words that do not occur in the grid.
   Words may occur multiple times in a grid. In this case, each occurrence should be reported on a separate line of the output.
   In file words.txt, each line contains exactly one word.
   Please use String method trim to remove leading or trailing whitespace when reading words.

Currently on part A. Code so far:

Result...:

Literally just blank lines.
I read some guides and it seemed like this was the proper format of printing an array but it doesn't make sense to me. The array I have is called "grid" so I would want to save contents of file to it and then print it out, right? So I don't understand why I have to make a new array with a new name (gridFileArray) but then still print using "grid". I tried printing "gridFileArray" but got more errors. I'm so confused right now.
I checked if the file is properly read by just adding a print statement inside the initGrid method instead of relying on the printGrid method like:

the result was just each character printed on a new line instead of a 10 by 10 grid.
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: 5 hours left and I'm still struggling with this lol


Result:
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another update...

Result:


So now it prints the first line of the grid correctly but not all 10 lines...
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's the expected output?  Print all rows of first column the all rows of second column like this or print all columns of first row the then all columns of second row like this?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This was the grid needed. I finally got it.
Only part B left now. If doable.
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are able to print it, you should be able to search as well.  Logic goes almost same.  Using a loop you printed, use a similar kind of a loop and search
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Getting this:
Exception in thread "main" java.lang.NullPointerException

Idea was to make a list of rows and one of columns and then search the words horizontally and vertically like that.
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code is creating array of 10 elements.  But each array element is not initialized, that's why you are getting NPE in the following lines



 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does your word.txt file look like, can you show us sample data?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I've updated:

The word file is as such:
have
that
they
with
this
from
will
make
etc.

I realised now I can't just search with each given word because there's thousands of them... so there must be another approach but I don't see how.
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So each line has a single word?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you might be able search row wise only no questions of columns, because you don't have multiple columns, am I correct?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I searching the grid or am I searching the word list?
The exercise requirement seems to ask that I search the grid for each word not the other way around.
"For each word, it should search the grid for a horizontal (left-to-right) or vertical (downwards) occurrence. If the word is found in the grid, then it should display the details of the occurrence in a format such as below:
grammar: row 8, position 1"

So my logic was that I have to see if each word can be found in the grid, thus have to search the grid left to right and downwards. The grid has 10 rows and 10 columns.
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me understand your requirement once again, it reads a word from the words file and searches in the grid am I correct?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It has to read each word from the words.txt file and search for each word in the grid. If the word is found, it has to print something like "word: row 4, position 1" to identify where in the grid the word was found.
 
Maja Zecevic
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you change in printGrid to get all 10 rows printed?
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This.
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any advice on how to actually search through the grid and finalise my project in the next 40 minutes? lol
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
 
Swastik Dey
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change the file path.

 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:try this


That code doesn't look right; it doesn't match what the requirements say.
 
Suane Mane
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:try this


What does this do? Add elements to position [0][0], [0][1], etc? I'm supposed to read the words from file, not add them manually.
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the methods already specified in the requirements, I would break down the problem further into these different tasks:

1. Create a string from a column of the char[][] array -- use new String(char[]) for this
2. Create a string from a row of the char[][] array -- use new String(char[]) for this
3. Check if a string contains a word: use String.indexOf() for this

I would have these working variables:
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program can be written without a char[][] array. In fact, specifying that you should use one leads to code that is more complicated than what's needed.

I ended up just writing a method to extract strings for the columns
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suane Mane wrote:Exercise requirement:
This exercise involves writing a program to solve word search puzzles. Create a Java class WordFinder with code as below.


That is horrible code to start with. Those fields don't need to be public. I just can't understand why these exercises have to be so prescriptive. Back when I was learning how to program, we were never given this much detail in our machine problems (that's what our programming assignments were called).
 
Piet Souris
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I ended up just writing a method to extract strings for the columns


'transpose' is an unfortunate name. If rows = { {"ab"}, {"cd"} } (2 x 1) then 'transpose' (in the mathematical sense) would yield { {"ab"}, {"cd"} } again (only 1 x 2), but the columns should be { {"ac"}, {"bd"} }. So maybe something like 'deriveColumns(rows)' would be a better name.
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose if you look at it the way you are, you're right. But I'm looking at it from a char-for-char basis, as though the String[] were actually defined as a char[][]. If it's really that surprising, I could go with transposeLetters()

There's a similar exercise on http://exercism.io (a great site, btw) where it's required to search left-to-right, right-to-left, vertical, and diagonal. I'm going to stick with transposeLetters()   — at least until a better one is suggested or comes to mind.
 
Piet Souris
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough! And I like the name as well. And a good puzzle site is always welcome, I'll have a look.

Sorry about being picky. I was doing a HackerRank course on statistics the other day, pretty basic, except for the last question, where I had to invert a 38 x 38 matrix. Python and R users were very much in an advantageous situation here, since that multiple regression was only one line of code in these languages. So I wrote my own Matrix class, and there it matters if you have a 3x1 matrix or a 1x3. And transposing happens a lot.

But if you do want to juggle with 2D (sorry Campbell, I mean array of   ) arrays, have a look at the '2048' game. Google for it.
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:have a look at the '2048' game. Google for it.


I have played that game many times. In fact, I've gotten 4096 at least once. Yes, it would be a good exercise for dealing with grid-like data structures.

Edit: although my memory might be misleading me. I know I've broken past 2048 at least a couple of times in the hundreds of times I've played that game. That I'm sure of. Maybe I'm remembering 4096 as the next goal.
 
Piet Souris
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehhh.... don't tell anybody, but my record is 512
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:a good puzzle site is always welcome, I'll have a look.


I'm sure you'll like it. Many different language tracks. Every problem also come with a unit test, which of course is 1000+ bonus points for me.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!