Computer Science Canada Reading from file. |
Author: | Krocker [ Wed Nov 13, 2013 3:09 pm ] | ||
Post subject: | Reading from file. | ||
Hiya, so i have to create a program that reads the number of lines, words and characters from the txt file.
So i managed to get the right number of lines, but then when it goes to the words, it always displays 0 number of words. I cant seem to figure this out and this is my 1st time working with reading files in java so please take it easy on me. |
Author: | DemonWasp [ Wed Nov 13, 2013 3:52 pm ] |
Post subject: | RE:Reading from file. |
Once you've read all the lines, there will be no more lines (or words, or characters) remaining in the file. You will have to count them while you are counting the lines in the file. |
Author: | Krocker [ Wed Nov 13, 2013 4:15 pm ] |
Post subject: | RE:Reading from file. |
So when i do the loop over, it doesnt start from the beginning of the file? Ok, i have tried a variety of ways to find the number of words, within the isNextLine() loop. But i either get a larger number of words, noelementfound ecxeption or to little number of words. Can someone please show me a way to do it, atleast show me the starting point? |
Author: | DemonWasp [ Wed Nov 13, 2013 8:44 pm ] |
Post subject: | RE:Reading from file. |
You can make a second Scanner object from the "line" string. Using that lineScanner, you can ask it .hasNext() until you hit the end of the line. Make sure that you add up all the numbers of words in each line. For characters, sum the values given by line.length(). |