Solved

kindly help me out on JAVA

Posted on 2003-12-11
25
461 Views
Last Modified: 2010-04-17
Prog with Java:
A prog that reads an Ascii file and lists all distinct words in that file with individual word count.

well i have done to some extent but yet to finish. help me to complete.
i am attaching the code:

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

class FileReadDemo
{
      public static void main(String arg[]) throws Exception
      {
      int size=0;
              // read from file....
      InputStream f=new FileInputStream("/rxt.txt");
      
      ArrayList a1=new ArrayList();
      
      size=f.available();
      byte buffer[]=new byte[size];
      f.read(buffer,0,size);
      String ss=new String(buffer,0);
      
      StringTokenizer st=new StringTokenizer(ss);
      
      String[] array=new String[st.countTokens()];
      int i=0;
      while(st.hasMoreTokens())
      {
      array[i]=st.nextToken();
       
      i++;
      }      
      // sort out ...help me here..

        for(int k=0;k<array.length;k++)
        {
        for(int j=k+1;j<array.length;j++)
      {
        if(array[j].compareTo(array[k])==0)
      {
      a1.add(array[j]);
      }
        }
        }
      System.out.println(a1);
      }
}
0
Comment
Question by:sandipmurmu
  • 10
  • 8
  • 7
25 Comments
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9926007
Use another ArrayList or an array for storing the counts too.

ArrayList count = new ArrayList () ;
..
..
for ( int i = 0 ; i < array.length ; i ++ )
{
  if ( ! a1.contains ( array[i] ) )
  {
    a1.add ( array[i] ) ;
    count.add ( new Integer ( 1 ) ) ;

  } // end if

  else
  {
    int j = a1.indexOf ( array[i] ) ;
    int currentCount = ((Integer) count.get ( j )).intValue () ;
    count.set ( j, new Integer ( currentCount + 1 ) ) ;

  } // end else

} // end for

While displaying, display the elements of a1 along with the elements of count -> the elements of count will hold the number of times the corresponding elements in a1 appeared in the array.

Mayank.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9928152
Use a Hashtable to store key-value pairs (where the key is the word and the value is the count).

Hashtable wordFrequency = new Hashtable();
.
.
.
    String curTok = null;
    Integer curCount = null;
    while(st.hasMoreTokens())
    {
        curTok = st.nextToken();
        if (wordFrequency.containsKey(curTok))
        {
            curCount = (Integer)wordFrequency.get(curTok);
            curCount = new Integer(curCount.intVal() + 1);
        }
        else
        {
            curCount = new Integer(1);
        }
        wordFrequency.put(curTok, curCount);
     }    
 
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9928241
There's a typo (intVal) in that last comment, but since you've got quite close to the solution you need, I thought it would be worth providing the rest.  Hopefully, this will also help you with the file reading part.  I think you had gone off down the wrong track with the InputStream.

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

class FileReadDemo
{
    public static void main(String arg[]) throws Exception
    {
        // read from file....
        BufferedReader br = new BufferedReader(new FileReader("rxt.txt"));

        Hashtable wordFrequency = new Hashtable();
        String line = null;
        while ((line = br.readLine()) != null)
        {
            StringTokenizer st = new StringTokenizer(line);
            String curTok = null;
            Integer curCount = null;
            while (st.hasMoreTokens())
            {
                curTok = st.nextToken();
                if (wordFrequency.containsKey(curTok))
                {
                    curCount = (Integer)wordFrequency.get(curTok);
                    curCount = new Integer(curCount.intValue() + 1);
                }
                else
                {
                    curCount = new Integer(1);
                }
                wordFrequency.put(curTok, curCount);
            }                
        }
       
        System.out.println(wordFrequency);
    }
}


Note that there are some other issues that you haven't considered yet.  You are not taking any account of punctuation or case, so if you had the following line in the file:

Random words in a file of random words.

Each word would only be counted once:

    Random != random  (because the case of the first letter is different)
    words != words.      (because of the period at the end)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9934019
Yeah, Hashtable is a good idea. I'd thought of it, but then for some reason, I thought not to change sandipmurmu's code, but just to add something to it so that it works with least modifications. Probably he's new to Java (based on his other questions).

By the way, sandipmurmu, I see that you have plenty of open questions. How about rating them (and this one, if our comments helped)?

Mayank.
0
 

Author Comment

by:sandipmurmu
ID: 9936893
thnx jimmack,
you have done the program..
Next I need to display the output in descending order of the
frequency of words..

sandip
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9937152
Well, I've made the required changes, but I'm not sure about posting it.  This looks like a homework question and I think that it would be obvious to a teacher that it isn't your own work (it uses an inner class that implements Comparable and overrides equals() to allow comparison and sorting within a colection).  Perhaps it would be better if you post the assignment question and then we can work through it with you.  This way, the experts here can be confident that you have understood the solution and that we haven't simply done your homework for you.

http://www.experts-exchange.com/help.jsp#hi105
0
 

Author Comment

by:sandipmurmu
ID: 9938235
how to sort values and not keys of a TreeMap..
I have constructed TreeMap..
0
 

Author Comment

by:sandipmurmu
ID: 9938554
The ouput could be in a descending order...
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9938872
You can't sort on values.  This is why I rewrote it using an ArrayList instead of a Hashtable.

The first thing you should do is create a new class to store the words and counts.  Change the Hashtable to an ArrayList to store these objects.  You'd need to change the code I posted to use contains() instead of containsKey() and change the way in which the word objects are accessed/created slightly, but otherwise the code is reasonably similar:

        ArrayList wordFrequency = new ArrayList();
        String line = null;
        while ((line = br.readLine()) != null)
        {
            StringTokenizer st = new StringTokenizer(line);
            Word curWord = null;
            while (st.hasMoreTokens())
            {
                curWord = new Word(st.nextToken());
                if (wordFrequency.contains(curWord))
                {
                    curWord = (Word)wordFrequency.get(wordFrequency.indexOf(curWord));
                    curWord.setCount(curWord.getCount() + 1);
                }
                else
                {
                    wordFrequency.add(curWord);
                }
            }                
        }

This relies on a class "Word" that has two attributes, word and count.

In order for the contains() and indexOf() methods to work, you need to override the equals(Object o) method to return true if the supplied object (o) contains the same word as the current object.

When you've got this working, you need to add the code to do a sort.  You can use the Collections.sort() method to sort the wordFrequency ArrayList:

    Collections.sort(wordFrequency);

However, for this to work, you will need to implement the Comparable interface in the Word class.  This involves implementing the compareTo() method.  This is a little bit like the equals() method, but in this case it returns an integer and you need to compare the counts, rather than the words.
0
 

Author Comment

by:sandipmurmu
ID: 9938894
here i am attachmy code. well i want to show the output
in descending order of the freqency...
ie..words :6
    .....  :3
     .....: 2
.........

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

class FileReadDemo
{
    public static void main(String arg[]) throws Exception
    {
        // read from file....
        BufferedReader br = new BufferedReader(new FileReader("/rxt.txt"));

        HashMap wordFrequency =new HashMap();
        String line = null;
      
        while ((line = br.readLine()) != null)
        {
            StringTokenizer st = new StringTokenizer(line);
            String curTok = null;
            Integer curCount= null;
            while (st.hasMoreTokens())
            {  
             
                curTok = st.nextToken();
                if (wordFrequency.containsKey(curTok))
                {
                    curCount = (Integer)wordFrequency.get(curTok);
                    curCount= new Integer(curCount.intValue() + 1);
                   
            }  
                else
                {
                    curCount = new Integer(1);
                   
            }
            
                wordFrequency.put(curTok, curCount);
           }                
        }
        System.out.println("\n");
        System.out.println(wordFrequency);
     
        //Get a set of the Entries
       Set set=wordFrequency.entrySet();
      
      //Get Iterator
        Iterator itr=set.iterator();

      //Display
        while(itr.hasNext()) {
            Map.Entry me=(Map.Entry)itr.next();
            System.out.print("\t" + me.getKey() + " : ");
            System.out.println(me.getValue());
            }
            System.out.println();
}

}
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9939059
I notice that the times of our last two posts are quite close together.  Have you had chance to read my last comment?
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 125 total points
ID: 9939865
Ok, its good to see that you came up with the coding efforts. I have shaped up your code a little, and I guess this should work. I'm using two more classes - don't get scared, they're small ones. The first one is this:

class MyEntry
{
      private String sWord ;
      private int iCount = 0 ;

      public void setWord ( String sWord )
      {
            this.sWord = sWord ;

      } // end of setWord ()

      public String getWord ()
      {
            return sWord ;

      } // end of getWord ()

      public void setCount ( int iCount )
      {
            this.iCount = iCount ;

      } // end of setCount ()

      public int getCount ()
      {
            return iCount ;

      } // end of getCount ()

} // class defintion over


Its like a wrapper, which will store words along with their counts. The second class is one which implements Comparator:


class MyComparator implements Comparator
{
      public int compare ( Object objFirst, Object objSecond )
      {
            return ( ( MyEntry ) objSecond ).getCount () - ( ( MyEntry ) objFirst ).getCount () ;

      } // end of compare ()

} // class definition over


Now, instead of a HashMap that you have used, I've simply used a Hashtable. And my code is like:

MyEntry objEntry = null ;

if ( htWordFrequency.containsKey ( sCurTok ) )
{
      objEntry = ( MyEntry ) htWordFrequency.get ( sCurTok ) ;
      objEntry.setCount ( objEntry.getCount () + 1 ) ;

} // end if

else
{
      objEntry = new MyEntry () ;
      objEntry.setWord ( sCurTok ) ;
      objEntry.setCount ( 1 ) ;
      htWordFrequency.put ( sCurTok, objEntry ) ;

} // end else


Hope you can make the changes in the variable-names and figure out which part of the code this refers to. Sorting would be as simple as:


ArrayList alEntries = new ArrayList ( htWordFrequency.values () ) ;
MyComparator objComp = new MyComparator () ;
Collections.sort ( alEntries, objComp ) ;


Then you can access the MyEntry objects from the ArrayList using the get () method and print the values.

Mayank.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sandipmurmu
ID: 9940359
well i have to implement Map.Entry Interface..
so that i can display
  the: 6
    a:2
    are:1
where the no. indicates frequency of the words in decreasing order..
this is really challenging me..
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9940507
Is it necessary to use Map.Entry? You can display the elements from the ArrayList which I showed, as:

for ( int iCount = 0, iSize = alEntries.size () ; iCount < iSize ; iCount ++ )
{
    objEntry = ( MyEntry ) alEntries.get ( iCount ) ;
    System.out.println ( objEntry.getWord () + ": " + objEntry.getCount () ) ;

} // end for

Does exactly the same thing.

rgds,
Mayank.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 125 total points
ID: 9940837
Go on then ;-)  Here's mine :-)

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

class FileReadDemo2
{
    public static void main(String arg[]) throws Exception
    {
        // read from file....
        BufferedReader br = new BufferedReader(new FileReader("rxt.txt"));

        ArrayList wordFrequency = new ArrayList();
        String line = null;
        while ((line = br.readLine()) != null)
        {
            StringTokenizer st = new StringTokenizer(line);
            Word curWord = null;
            while (st.hasMoreTokens())
            {
                curWord = new Word(st.nextToken());
                if (wordFrequency.contains(curWord))
                {
                    curWord = (Word)wordFrequency.get(wordFrequency.indexOf(curWord));
                    curWord.setCount(curWord.getCount() + 1);
                }
                else
                {
                    wordFrequency.add(curWord);
                }
            }                
        }
       
        Collections.sort(wordFrequency);
        System.out.println(wordFrequency);
       
        Collections.reverse(wordFrequency);
        System.out.println(wordFrequency);
    }
   
    static class Word implements Comparable
    {
        private String word;
        private int count;
       
        public Word(String word)
        {
            this.word = word;
            this.count = 1;
        }
       
        public String getWord()
        {
            return (word);
        }
       
        public int getCount()
        {
            return (count);
        }
       
        public void setCount(int count)
        {
            this.count = count;
        }
       
        // So that "contains()" works
        public boolean equals(Object o)
        {
            boolean result = false;
            if (o instanceof Word)
            {
                Word comp = (Word)o;
                if (comp.word.equals(word))
                {
                    result = true;
                }
            }
           
            return (result);
        }
       
        // So that "Collections.sort()" works
        public int compareTo(Object o)
        {
            int result = 0;
            if (o instanceof Word)
            {
                Word comp = (Word)o;
                result = this.count - comp.count;
            }
           
            return (result);
        }
       
        public String toString()
        {
            return (word + " " + count);
        }
    }
}
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9940850
Hmm.... not bad :-)

BTW, why over-ride equals () and compareTo (), etc.... just one line of code in MyComparator does it. But yeah! The number of classes is less :-)

Mayank.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9940856
>> just one line of code in MyComparator does it

I mean - only by implementing the compare () method which has a single-line :-)

And yes! The Hashtable can be removed totally.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9940885
Storing Word (or MyEntry) objects in an ArrayList prevents the use of containsKey().  Instead, I had to use contains().  I overrode the equals() method so that contains() would return objects where the string matched (and ignored the count).

Basically, equals() is for finding the word in the ArrayList and compareTo is for sorting.

(I've just read through this again and I'm not sure if it's all that clear.  Let me know if I'm confusing things ;-))
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9941073
No no, its not confusing at all (can't say that about Sandip). I know why you wrote them.
I was just wondering whether it'll be ok for him to over-ride so many methods, etc., if he's a beginner. That's why I thought of making my own Comparator. Also, first that compare () method had 3-4 lines, then I reduced it to one return statement :-)

But of course, using only the ArrayList is better than using a Hashtable too.

Mayank.
0
 

Author Comment

by:sandipmurmu
ID: 9943908
hi jimmack and mayank,
you both came up to the end of the program..
well both codes are excell...
i have followed mayank to some extent...and it worked..
i will do the same with jimmack's codes too..

thnx its a great help..i will be back soon.
sandip
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9946753
Glad to help :-) go ahead and try some stuff.
0
 

Author Comment

by:sandipmurmu
ID: 9951619
no stops guys..
ok! sorry for being informal.

can you all suggest me some good books on j2ee..

bye
sandip
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9953014
;-)

It depends on which parts of J2EE you're interested in :-)

I've only dealt with JSP/Servlets (ie. not Enterprise Java Beans).  For JSP/Servlets I would highly recommend "Core Servlets and Java Server Pages" by Marty Hall and Larry Brown.  http://www.coreservlets.com.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9954614
For EJBs, try:

1. Head First Ejb: Passing the Sun Certified Business Component Developer Exam by Kathy Sierra, Bert Bates (Publisher: O'Reilly)

2.  J2EE Security for Servlets, EJBs, and Web Services by Pankaj Kumar (Publisher: Prentice Hall)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9954618
Or Mastering Enterprise Java Beans by Ed Roman.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now