Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Learning Java help for greenhorn developer  RSS feed

 
Niel McEdwards
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello forum members,

I am currently working on the following project for school:

"Find all the words in a dictionary that contain exactly five vowels (a, e, i, o and u) in ascending order."

I believe that with using an inputstreamreader and then saving the found words in a arraylist, I could then sort them in ascending order. But how do I load an external file to the java program? How will it know where each word starts, how to distinguish the actual word from it's definition?

I appreciate your comments and help as to how I can start to layout the solution for this project. Any help is much appreciated.

Thank you.

Niel
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niel McEdwards wrote:Hello forum members,

I am currently working on the following project for school:

"Find all the words in a dictionary that contain exactly five vowels (a, e, i, o and u) in ascending order."

I believe that with using an inputstreamreader and then saving the found words in a arraylist, I could then sort them in ascending order. But how do I load an external file to the java program? How will it know where each word starts, how to distinguish the actual word from it's definition?

I appreciate your comments and help as to how I can start to layout the solution for this project. Any help is much appreciated.

Thank you.

Niel


A bit of searching found this
But that would require you to parse XML. If the purpose of your assignment is just to prove a method of finding vowels rather than handling potetially huge data sets can you not just set up a few words in a flat text file (no definitions) and read that in using a file reader?

I tried googling "excel file of english words" and found some stuff which you could potentially (maybe with some reformatting) save to a .csv and read in using a file reader and string tokenizer.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First word of advice: decompose the problem into different parts, where each part has a separate concern. You have at least two "parts" here with different concerns: a Dictionary and a List of words that you need to sort. BTW, the way the requirement is stated, it wasn't clear to me whether the list of words had to be sorted in ascending order or if the words that qualified had to have the vowels in that particular order. Since the words are coming from a Dictionary, I assumed the words would already be in ascending order so all you really need to do is to go through the words in the natural order that they appear in any dictionary.

The main job of the Dictionary to provide words. You'd probably ask it something like nextWord(). Or you could ask it nextWord(criteria) where criteria is something that evaluates a condition and nextWord() will only return a word that satisfies the condition; this is also known as a Predicate (search for org.apache.commons.collections). To give you an idea of what this might look like:

Then you have the list of words. Assuming the Dictionary gives back words in the proper order, the list should already be sorted. If for some reason this isn't the case, then the java.util.Collections class can sort the list for you.

As for your questions on the implementation details of the dictionary and how to read and parse its entries, you really need to see exactly what the data looks like first. Then look in the java.io package to see which classes will help you get the job done.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Neil and welcome to the Ranch!

In order to get the best out of the Ranchers who are always willing to help others, you should TellTheDetails (or at least some more details). For start, it would be ok to know what kind of file you have to store dictionary words and what is the format of the file (how words are organized). In the meantime, you might want to check Reading, Writing and Creating Files tutorial in order to get an idea how I/O works in Java. The tutorial has some examples that you can try working out.

Since this community is NotACodeMill nobody is going to implement any code for you, which is a good approach, because it will make you learn the most from the guidelines you get here. You need to ShowSomeEffort, and then we can all discuss about the issues of your approach.

What I would suggest you, is (before everything else) you take a pencil and paper and write steps in your native language that would help you solve the problem. Once you have an idea how that could be done, you can post it here (also as list of steps in English) for further discussion. And once you verify the process of solving a problem, you can then move to Java and start implementing your idea in baby steps.
 
Niel McEdwards
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So far I am using regex to find a match. My code is as follows:



It matches a couple of words in my dictionary.txt file; almost like it stopped looking for more words. It maybe I am lacking knowledge in Regex, or how can I make sure it looked at all the lines of the dictionary.
It found:
aah
aahed

Any help is greatly appreciated.

Thank you.

Niel
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niel McEdwards wrote:It matches a couple of words in my dictionary.txt file; almost like it stopped looking for more words. It maybe I am lacking knowledge in Regex, or how can I make sure it looked at all the lines of the dictionary.
It found:
aah
aahed


It is not a regex issue. It is a logic issue. You are reading the first line for the regex, but not processing it. And you are processing the other lines based on what you find with the first line. Basically, you are doing something like this...


Read the first line into the regex.
if find a match with the first line, then process the second line.
if find another match with the first line, then process the third line.
if find another match with the first line, then process the fourth line.
etc.


So, you code never processes the first line. And it processes only the number of lines, depending on how many "a" does it find in the first line.

Henry
 
Niel McEdwards
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for giving me some feedback.

I am lost, because as far I can see in my code, the while loop would go through the entire dictionary and the streamreader goes line by line.

If possible I could get some more help, or maybe I need to look at this from another programming angle.

Thank you.

Niel
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niel McEdwards wrote:
I am lost, because as far I can see in my code, the while loop would go through the entire dictionary and the streamreader goes line by line.

If possible I could get some more help, or maybe I need to look at this from another programming angle.


Looping through the entire dictionary and reading it line by line doesn't mean that it is being processed. Maybe I can modify my description...

Read the first line into the regex.
Read second line. If find a match with the first line, then process the second line.
Read third line. If find another match with the first line, then process the third line.
Read fourth line. If find another match with the first line, then process the fourth line.
etc.


Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!