?
Solved

read input file's data in 2 parts? (file's separated by a #)

Posted on 2003-03-31
5
Medium Priority
?
168 Views
Last Modified: 2010-03-31
Someone help me with this(pls)! @_@
it should be fairly simple for most programmers...

let's say this is my input file:

//i want to write a program which calculates word frequency
//--------------------------------------------------------

Hello this is a test  //lines 1-3 are the lines i want to read
This appears three times in this test
some words contain digits such as 10mar 2003
#                    // this works like a divider -- below are some words to calc freq
This                 // find the word frequency of "This"
test                 // find the word frequency of "test"
10mar                // find the word frequency of "10mar"
2003                 // find the word frequency of "2003"

//end of file
//--------------------------------------------------------------
Sample output:
THIS:3
TEST:2
10MAR:1
2003:1

------------------------------------------------------
Now how can I read only the top part and not count the word frequency in the bottom part?
Example: By right, "this" only appears 3 times, but I'm unable to write the code such that it skips counting the bottom "this" as well. Can someone teach me with the code to do this?
0
Comment
Question by:honeybee_nus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
makas earned 120 total points
ID: 8243561
Here's a non optimized code. Will definitely do what you wanted.

public class WordCounter
{
    public static void main( String[] args )
        throws Exception
    {
        if( args.length == 1 )
        {
            String filename = args[0];
            FileReader fr = new FileReader( filename );
            countWords( fr );
        }
        else
        {
            System.err.println( "Please specify the file name" );
            System.exit(1);
        }
    }
   
   
    public static void countWords( Reader reader )
        throws Exception
    {
        BufferedReader br = new BufferedReader( reader );
        String str = null;
        HashMap counter = new HashMap();
        boolean reachedLastLine = false;
        while( ( str = br.readLine() ) != null )
        {            
            if( !str.equals( "#" ) && !reachedLastLine )
            {
                countWordsInLine( str, counter );
            }
            else if( reachedLastLine )
            {
                String word = str.toUpperCase();
                System.out.println( word + ":" + counter.get( word ) );
            }
            else
            {
                reachedLastLine = true;
            }
        }
        br.close();
    }
   
    public static void countWordsInLine( String str, HashMap counter )
        throws Exception
    {
        StringTokenizer st = new StringTokenizer( str );
        while( st.hasMoreTokens() )
        {
            String token = st.nextToken().toUpperCase();
            Integer i = (Integer)counter.get( token );
            if( i != null )
            {
                i = new Integer( i.intValue() + 1 );
            }
            else
            {
                i = new Integer( 1 );
            }
            counter.put( token, i );
        }
    }
}
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8243680
homework?
0
 

Expert Comment

by:CleanupPing
ID: 9058360
honeybee_nus:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9769979
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to makas

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question