Link to home
Start Free TrialLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

hash map program.

I am working on a program in java to do the following:

File.txt contents:
PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin
===
PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin1
===


output:
PASS 2  logs:/usr/local/bin,logs:/usr/local/bin1
FAIL 2  logs:/usr/local/bin1
FEATURE_NOT_SUPPORTED 1      logs:/usr/local/bin
 
so I have 100 to 800 results that are written to a file.
Each word in the text has a meaning.(like pass.fail to name few I can have different names).
I am looking for the program which reads the file
2) reads each word keeps in a array
3) write the location of the logs.
4) number of times that key word is used.
I am looking like a 3 dimensional table which will print all the results.




Avatar of msk_apk
msk_apk
Flag of India image

create an object that has the below fields
public class Word
{
         String name;
         int count;
         String fileName;
}
On reading each word, store that object in a HashMap. HashMap's key is name and value is Word object.
Avatar of sigma19

ASKER

i tried that but could not complete as I keep getting different Exceptions...
I have a plan for the logic issue which is correct..but never worked in java
this is the code;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class HashMapProgram {

public HashMapProgram() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("c:\\temp\\test\\file.txt"));
        String buff = null;
         String log = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(!alw.contains(s))alw.add(s);
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }
            


        }
        in.close();

        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Integer ii = (Integer)m1.get(s);
            System.out.print("" + ii + "  ");
            ArrayList logg = (ArrayList)m2.get(s);
            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                System.out.print(ss + "; ");


            }
            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }


    



    }

    public static void main (String [] args){
        new HashMapProgram();
    }

    
}

Open in new window

This is the output.

Check if it is correct.


PASS  4  /usr/local/bin; /usr/local/bin; /usr/local/bin1; /usr/local/bin1; 
FAIL  4  /usr/local/bin; /usr/local/bin; /usr/local/bin1; /usr/local/bin1; 
FEATURE_NOT_SUPPORTED  1  /usr/local/bin; 
ENHANCEMENT  2  /usr/local/bin; /usr/local/bin1; 
NOT_EXECUTED  2  /usr/local/bin; /usr/local/bin1; 

Open in new window


You probably don't need to repeat the logs as amny time as it appers in the log file?

This is without repetition:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class HashMapProgram {

public HashMapProgram() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("c:\\temp\\test\\file.txt"));
        String buff = null;
         String log = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(!alw.contains(s))alw.add(s);
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        if(!all.contains(log))all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }
            


        }
        in.close();

        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Integer ii = (Integer)m1.get(s);
            System.out.print("" + ii + "  ");
            ArrayList logg = (ArrayList)m2.get(s);
            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                System.out.print(ss + "; ");


            }
            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }


    



    }

    public static void main (String [] args){
        new HashMapProgram();
    }

    
}

Open in new window



PASS  4  /usr/local/bin; /usr/local/bin1; 
FAIL  4  /usr/local/bin; /usr/local/bin1; 
FEATURE_NOT_SUPPORTED  1  /usr/local/bin; 
ENHANCEMENT  2  /usr/local/bin; /usr/local/bin1; 
NOT_EXECUTED  2  /usr/local/bin; /usr/local/bin1; 

Open in new window



Words ordered alphabetically

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;

public class HashMapProgram {

public HashMapProgram() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("c:\\temp\\test\\file.txt"));
        String buff = null;
         String log = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(!alw.contains(s))alw.add(s);
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        if(!all.contains(log))all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }
            


        }
        in.close();

        Collections.sort(alw);
        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Integer ii = (Integer)m1.get(s);
            System.out.print("" + ii + "  ");
            ArrayList logg = (ArrayList)m2.get(s);
            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                System.out.print(ss + "; ");


            }
            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }


    



    }

    public static void main (String [] args){
        new HashMapProgram();
    }

    
}

Open in new window



ENHANCEMENT  2  /usr/local/bin; /usr/local/bin1; 
FAIL  4  /usr/local/bin; /usr/local/bin1; 
FEATURE_NOT_SUPPORTED  1  /usr/local/bin; 
NOT_EXECUTED  2  /usr/local/bin; /usr/local/bin1; 
PASS  4  /usr/local/bin; /usr/local/bin1; 

Open in new window



Output formatted exactly like in your example.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;

public class HashMapProgram {

public HashMapProgram() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("c:\\temp\\test\\file.txt"));
        String buff = null;
         String log = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(!alw.contains(s))alw.add(s);
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        if(!all.contains(log))all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }
            


        }
        in.close();

        Collections.sort(alw);
        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Integer ii = (Integer)m1.get(s);
            System.out.print("" + ii + "  ");
            ArrayList logg = (ArrayList)m2.get(s);
            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                if(jj<logg.size()-1)
                System.out.print("logs:" + ss + ",");
                else
                    System.out.print("logs:" + ss); 


            }
            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }


    



    }

    public static void main (String [] args){
        new HashMapProgram();
    }

    
}

Open in new window



ENHANCEMENT  2  logs:/usr/local/bin,logs:/usr/local/bin1
FAIL  4  logs:/usr/local/bin,logs:/usr/local/bin1
FEATURE_NOT_SUPPORTED  1  logs:/usr/local/bin
NOT_EXECUTED  2  logs:/usr/local/bin,logs:/usr/local/bin1
PASS  4  logs:/usr/local/bin,logs:/usr/local/bin1

Open in new window

Avatar of Mick Barry
Reading the words from the file is fairly straight forward, and you can store them as you read them.
Not clear on the processing required, so let me know and I'll steer you in the right direction. Looks to be fairly simple


Avatar of sigma19

ASKER

Thanks Yan. it works,

1) can you pl explain me the logic ?

2) why did you not use a class and create all these as objects to it? so that it will be expandable ..like for example I can add the data also when we write to the screen.

PASS  4  logs:/usr/local/bin,logs:/usr/local/bin1 26thApril 2010.

1) The logic is starightforward - we setup two hastables - one will keep
our target word as key and number of times this word was encountered wrapped iun the Integer
as a value;
another hashtable  will have again the world as a key and arraylist of log files where it appears
as a value
Then we go through the file
and when we encounter the line starting with log we set this
log as our current log for this piece of data, when we
encounter a line of words we remember them in the arraylist  of them,
when we encounter separator "==" - then
the analyze our arraylist of words just read , and as we go throughthis list
we populate both our hashtables;
the first hashtable - we just check if such word existed
we increment the Integer in the vlaue;
for population of  the second hashtable we go through
our current arraylist of words read in for this log file
and for each of these words we check - if this second hashtable
contains our current log in the arraylist (arraylist is the value retrivwed for the word),
we do nothing, if this is a new log file for this word then we add this log to the arrayl;ist
corresponding to this word and now we have a new value - longer arraylist corresponding to this word
in second hashtable;
if there was no such word in second hashtable we create a new array;ist and put ther first eelemnt
and put it to the hahstbale.


One note aside: in fact when we update arraylist we probably don't need to do second put
command on the hashtable again, becasue it just keeps pointrer to the arraylist.
I still prefer to have this command - it is easier to think of it this
way - small loss of time - again if you do millions of reacords - then all these
thisng should be reconsidered.

When we raead through all pieces then we have two hashtables with the same
array of keys - all our words are keys in both-  
- one hashtable where the values are Integer's representing the numbers of occurrences,
another  where values are arraylist of different log files,
which contain this word.

Now we just go through all our words and retieve the numbers of coccurresnces form first hashtable
and arraylists from the second;
  we expand the arraylists into the string of concatented logs
and print out one line for each word

one more aside note: Actaullay I could retrieve the keySet from either hastable
after their accumulation and use it to traverse
the hashtables; I perfer to set up one more arraylist (called
alw in this case) where I collect just unique words
along the way as we read - it is a little bit of waste of space and time
but for me it's kind of easier to think this way.
Once again,  if number of keys goes into milloiions
where it becomes essential, keySet
can be retrived from the hahsmap after their creation.
But when you do  millions - it is quite anither application.
In the majority of what I do in real life,
I never go into millions, so I tend to design it
differently for each case.
2) Well you can use a class, or not use a class, it really does not matter that much in this acse.
If you go to really big number of items, then class
would probably be easier to keep and oranize in  mind and in the typing,
(which is far less important for me) either;
with two items - I'd probably spend more time
typing constructor, set, get methods, than
in this way with two hashtables.

Once the framework is in place this code is no less
expandable - even though adding third hashtable
will require a little bit more of typing in this case, but in my experience
it is much less typing - it is mostly logic adn assciated debugging,
which takes most of the effort.
So now having the logic in mind i can add third hahstable
in no time because it will be analogious to the
perevious hahstable.

And i didn;t know that you want it to be expanded in this way.
As I said if you would offer from the beginning
the task which would require not two but ten fileds,
no question I'd be opting for the class.

in general I prefer to do taks at hand - in my real appliactions
all my plannings for generality turned out in most cases to be inadequate -
I was thinking they would in coming years build additional garage,
and in fact it turns out tha they want to add a second
storey to the house - so all my time spent on preparations for garage were just useless
pain. Unless you have written specification
which says what is the plan to expand, i concentrate on the task at hand
rather than trying to predicet how it will be generalzied in the future.
 
 

> 2) why did you not use a class and create all these as objects to it? so that it will be expandable ..like for example I can add the data also when we write to the screen.

thats a good idea, would make things a lot simpler
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sigma19

ASKER

Thanks yan.
few questions:
1) I dont think we need continue block in
 if(buff.startsWith("log")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
and
al = new ArrayList();
                continue;

==> I think we dont need the continue there as once it is read it will go in loop .
Sure, remove it.

It is good practice from my point of view to have.
And I can agree I have a little bit special attitude.
As I'm writing it - I realize, that after that point in this case I don't want to do anything in this loop,
so i put there continue, then I don't need to think, say, if I have if - else
construction, or suppose I don't have else, but have just if()..
without else, then without continue ther could happen to be a bug.
I prefer save thinking rather  than save typing.
Again, everything has its apropriate place - if you are doing something on the edge
of computer capability in a sense of memory or time, then
in certain pieces it makes sense to do more thinkning
and save some activity.
But this is very simple task in terms of reources and to type continue
is a safer practice and does not hurt.
Remove and try if it works fine (which it would, I hope, in this case, but
in some other these simple things may easily break, so it is better
to test after each change).
> ==> I think we dont need the continue there as once it is read it will go in loop .

you don't. In fact if you use the suggestion I posted earlier the code to read file can be simplified to only a few lines