Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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

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
honeybee_nus
Asked:
honeybee_nus
1 Solution
 
makasCommented:
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
 
Venci75Commented:
homework?
0
 
CleanupPingCommented:
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
 
girionisCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now