Results 1 to 7 of 7
  1. #1
    Drukqs is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default read char words lines

    Hello all in java land. I need some help figuring out how to read and display a txt file, the amount of words, the amount of lines and amount of char not including white spaces. I can open the file and display it but i dont have much else. heres what i got thus far...
    Java Code:
    package hw3;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    public class Hw3 {
    
    	public static void main(String[] args)throws Exception {
    				try {
    		     			BufferedReader in = new BufferedReader( new FileReader("file"));
    		   			String line;
    		     			while ( ( line = in.readLine() ) != null ) {
    		     				System.out.println( line );
    		     			}
    		     in.close();
    		     } catch (IOException e) {
    		     e.printStackTrace();
    		     }
    		     
    	}}

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    First of all, use a Scanner, BufferedReaders don't work well for files, they are more for command line programs. Then, create a while loop that adds the lines to an ArrayList (or Array, but that is far more difficult). Make the loop repeat until there are no lines left. You can then divide the lines into words, and count the total words, characters, and lines.

    Basically, you will end up with an ArrayList<String> containing lines, and probably a String[][] containing words.

    I'll try to create some sample code for you

    Good Luck,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So, you can do part one: reading in the file and displaying it, and now on to the other parts.

    Let's start with the line count: How do you think you may be able to count the lines in the file? Any guesses?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Singing Boyo View Post
    First of all, use a Scanner, BufferedReaders don't work well for files, they are more for command line programs.
    Java Code:
    $ java HowCanThatBe <input.txt

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Let's start with the line count: How do you think you may be able to count the lines in the file? Any guesses?
    Think that how you read the file? Do you read the file at once or.....

  6. #6
    sheuli is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default Hi,I am new in this forum

    Hi,
    I am new in this forum.I don't know how to post new thread.Please give me advice.

    regards,
    Sheul

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Welcome to our community.

    Please read our FAQ page. All what you is there. Please read it completely. Don't interact with others threads, if your question is not related with that. Start a new thread in the correct sub-forum. Good luck.

Similar Threads

  1. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM
  2. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  3. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 05:27 AM
  4. Scrambling Words
    By Shadow22202 in forum New To Java
    Replies: 9
    Last Post: 04-30-2008, 04:51 AM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •