Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reading from file.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: 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.

code:

import java.io.*;
import java.util.Scanner;

public class question1{

        //main method
        public static void main (String[] args) throws FileNotFoundException{
                Scanner userInput = new Scanner (System.in); //creates an input object
               
                System.out.print ("Enter input file name: ");
                String fileName = userInput.next();
               
                Scanner in = new Scanner (new File(fileName) );
               
               
                int count = 0;
                while (in.hasNextLine()) {
                       
                        String input = in.nextLine();
                        count++;
                }
               
                System.out.println ("Line: " + count);
               
               
                count = 0;
                while (in.hasNext()) {
                       
                        String input = in.next();
                        count++;
                }
               
                System.out.println ("Words: " + count);
        }
}


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.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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.
Krocker




PostPosted: 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?
DemonWasp




PostPosted: 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().
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: