Click to See Complete Forum and Search --> : [RESOLVED] Scanner help - split my code
highburycottage
11-02-2010, 09:31 AM
Hi,
I'm a mature student and require some assistance. I want to do this myself as I will never learn. I have been given an assignment to do, which I have done, but not as they want. So could somebody please look at my code below, along with what I have to do, and point me in the right direction. They want me to use a helper method but I'm not sure how to call it...
Outline.. a method boolean countWords to collect information in a file. This method will depend on openRead to initialize a scanner to read a file. This method should provide the results using the help method updateStatistics. The results are number of words in the file; the number of letters, the number of words with even and odd letters.....
The helper method void updateStatistics(String word), which updates the values of the statistic-holding instance variable to take account of the word received by this method as an argument.
My code:
import java.io.*;
import java.util.*;
public class WordCounter
{
//TODO add the required instance variables
public String fileName;
public Scanner sc;
public int totalWords, totalEven, totalOdd, totalLetters;
//TODO add the required constructor
public WordCounter(String inFile)
{
fileName = inFile;
totalWords = 0;
totalEven = 0;
totalOdd = 0;
totalLetters = 0;
}
//TODO add the openRead method
public boolean openRead() throws FileNotFoundException
{
sc = new Scanner(new File(fileName));
if (fileName != null)
{
return true;
}
else
{
return false;
}
}
//TODO add the countWords method
public boolean countWords() throws FileNotFoundException
{
boolean openFile = openRead();
if (openFile != true)
{
return false;
}
else
{
sc = new Scanner(new File(fileName));
String nextLine;
int allLetters;
while (sc.hasNext())
{
nextLine = sc.next();
allLetters =+ nextLine.length();
if (allLetters % 2 == 0)
{
totalEven++;
}
else
{
totalOdd++;
}
System.out.println(nextLine + " [" + allLetters + "]");
totalWords++;
totalLetters = allLetters + totalLetters;
}
sc.close();
return true;
}
}
//TODO add the updateStatistics method
public void updateStatistics(String word)
{
}
//TODO add the toString method
public String toString()
{
return "File analysed: " + fileName + "\n" +
"There were " + totalWords + " words and " + totalLetters + " letters" + "\n" +
"There were " + totalEven + " even words, and " + totalOdd + " odd words";
}
Now can somebody point in the right direction as to what is suppose to go in the helper method, and then how do I call it, everything I have done gives me an error as one is boolean and the other is string.
Any help much appreciated.
zimonyi
11-02-2010, 11:05 AM
Hello,
I have limited time at the moment but I can give you some pointers.
First off, your countWords() method calls openRead(). This method actually opens your file and sets it to the instance variable sc but then in countWords() you open it again. You only need to open it once.
Also, your openRead() method throws FileNotFoundException. You will never come down to your if-statements if fileName is null, because your new Scanner() call will cause a FileNotFoundException (or another Exception).
You should probably check the sc variable instead to see if it should be true or false.
Basically, I would guess that what your updateStatistics() method should contain is all of your variable increments, meaning your checks on the word.
What does Scanner.next() return? You declare the return value as nextLine but is that true? I would guess from your excersice that it should perhaps be one word? If so, the argument to your updateStatistics() might be more obvious.
Try it a bit and I will check tomorrow if you have come further.
Archie
your
highburycottage
11-02-2010, 01:56 PM
Hi Archie,
Thanks for those pointers and the tips.
I've done it!!!!!! Would you mind just checking the code I have put below to confirm that I have actually done it correctly.
Removed the second instance of opening the scanner.
Changed the check variable to that of the scanner.
After a about an hour of messing about - thanks for the guidance about the scanner.next() - got me thinking, finally managed to get the countWords() method to work. My updated method is:
public boolean countWords() throws FileNotFoundException
{
boolean openFile = openRead();
if (openFile != true)
{
return false;
}
else
{
String word;
while (sc.hasNext())
{
word = sc.next();
updateStatistics(word);
}
sc.close();
return true;
}
}
And my updated and working updateStatistics(String word) method is:
public void updateStatistics(String word)
{
int allLetters;
while (sc.hasNext())
{
word = sc.next();
allLetters =+ word.length();
if (allLetters % 2 == 0)
{
totalEven++;
}
else
{
totalOdd++;
}
System.out.println(word + " [" + allLetters + "]");
totalWords++;
totalLetters = allLetters + totalLetters;
}
}
If thats correct thanks for the pointers - they got me thinking and I have managed (I presume) to work it out.
zimonyi
11-02-2010, 05:36 PM
No I would say it is not correct. It will almost do what you want but not correctly.
My suggestion is that you do a System.out.println() in your while-loop in the method countWords() and print something useful.
Then you also put a System.out.println() in your while-loop in your updateStatistics() method and print something useful and see what I mean.
Archie
highburycottage
11-02-2010, 07:36 PM
Hi Archie,
Bummer!!!
Thanks for that, I see what you mean. I put a println() method with a meaningful statement, and my output was missing the first word from the file!!!
So I presume one of my hasNext() or word = sc.next() statements is moving the reader on before it actually prints out the first word.
I have moved the word = sc.next() within the helper method and it then prints the first word in the file, but then ignores the last :-(
I will have another go at it in the morning. Cheers
zimonyi
11-03-2010, 01:53 AM
Hello,
yes, you are correct.
Can you with words explain what sc.next() does?
Archie
highburycottage
11-03-2010, 03:32 AM
Archie,
I was under the impression that sc.next() read the next word of the file, put it through the loop, allowing it to display in the required format, along with how many letters were in the word, and increment whether it was an even or odd length word. Then word = sc.next() allocated the variable word the next word of the file and repeated the process.
By moving the word = sc.next() I get all sorts of different results, one gives me a constant loop of the first word Deep [4] and another reads the whole file, all the words, but when it comes to the last word, it does not perform the loop. I have put System.out.println() statements through both of the methods, from what I can see in the updateStatistics it takes the first word of the text file, but it is then dropped and changed for the next word when it gets to the word = sc.next(), so I have then moved this statement to the end of the method and it does all the file, but does not run the loop for the last word.
Am I on the right lines?
Paul
zimonyi
11-03-2010, 06:35 AM
Sort of.
In words, tell me the purpose of the countWords() method.
In words, tell me the purpose of the updateStatistics() method.
If you ignore what the code actually does (since it is not 100% correct) simply say what it should do.
Then when you have, in words, described both methods, see what the code does and if you can figure out what is incorrect.
Archie
highburycottage
11-03-2010, 06:59 AM
a method boolean countWords() to collect statistical information about the words in the file fileName. This method proceeds to process the words using the scanner sc and should result (see the helper method) in the instance data of the object storing:
the total number of words in the file.
the total number of letters in all the words.
the total number of words with an even number of letters.
the total number of words with an odd number of letters.
While reading the file the method displays on screen, on separate lines, each word it reads, with the number of characters in the word in [] , ie Deep [4]
is [2] etc....
When completed this method should close the scanner and return true.
The helper method void updateStatistics(String word), which updates the values of the statistic-holding instance variables to take account of the word received by this method as an argument.
My countWords method() should therefore take the word, pass it to the helper method, which should do the counting etc.. and then pass back the information to the countWords() method and allow that to output the software.
So I presume that is where I'm going wrong as I have my update method printing out the results instead of the countWords() method, or am I getting totally confused and it wrong?
zimonyi
11-03-2010, 07:24 AM
My countWords method() should therefore take the word, pass it to the helper method, which should do the counting etc.. and then pass back the information to the countWords() method and allow that to output the software.
So I presume that is where I'm going wrong as I have my update method printing out the results instead of the countWords() method, or am I getting totally confused and it wrong?
No, you are correct. You are currently printing in your helper method which is not correct.
Your countWords() method is the one that should do the printing.
The helper method void updateStatistics(String word), which updates the values of the statistic-holding instance variables to take account of the word received by this method as an argument.?
Compare your own updateStatistics() method with the above sentence. The above sentence is the requirement. Does you updateStatistics() method do exactly that? And if not, where does it differ?
For each word, how many times should you do the sc.next() call? I think you know the answer but you are not doing it in the correct place in your code as it looks now.
Archie
highburycottage
11-03-2010, 08:41 AM
Archie,
Thanks for that. I think I have done it this time. Every thing that should be there, is. The numbers add up - as they should. I believe that the amongst other things the while statement within updateStatisctics - was not needed.
My amended code is this:
public boolean countWords() throws FileNotFoundException
{
if (openRead() != true)
{
return false;
}
else
{
String word;
int allLetters;
while (sc.hasNext())
{
word = sc.next();
updateStatistics(word);
allLetters =+ word.length();
System.out.println(word + " [" + allLetters + "]");
}
sc.close();
return true;
}
}
This method, now as far as I can see, takes the word and sends it to the helper method, which update the statistics that it should. The method then works out the length of the word (as this is not a statistic holding variable) and prints the word, along with the length.
The update method is as follows:
public void updateStatistics(String word)
{
int oddEven;
totalWords++;
oddEven =+ word.length();
if (oddEven % 2 == 0)
{
totalEven++;
}
else
{
totalOdd++;
}
totalLetters = oddEven + totalLetters;
}
This method now only takes the word, increments the totalWords variable, decides if the word is made up of an odd or even number of letters, increments the correct variable and then finally increments the totalLetters variable.
Does this appear correct.
Thanks and regards,
Paul
zimonyi
11-03-2010, 09:46 AM
Yes, you have got it.
As you say, the while-loop withing the updateStatistics() method was not necessary (in fact incorrect) since it was to be done in the countWords() method.
One things to note though, you have created a local variable called allLetters that you are printing.
There is an instance variable that does the same thing, which you are also using in the updateStatistics() method. I think you should only use the instance variable instead of the local variable.
Archie
highburycottage
11-03-2010, 12:23 PM
Archie,
Excellent.
Thank you very much for your patience and guidance. I will take on board what you have put about what the methods should do and then try to go about doing it. It helped.
I have replaced the local variable allLetters with the instance variable totalLetters.
All your advice greatly appreciated.
Regards,
Paul
zimonyi
11-03-2010, 01:13 PM
Glad I could help.
Archie