• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

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.




0
vkchaitu82
Asked:
vkchaitu82
  • 9
  • 3
  • 3
  • +1
1 Solution
 
msk_apkCommented:
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.
0
 
vkchaitu82Author Commented:
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
0
 
for_yanCommented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
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

0
 
for_yanCommented:

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

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

0
 
for_yanCommented:


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

0
 
for_yanCommented:


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

0
 
objectsCommented:
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


0
 
vkchaitu82Author Commented:
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.

0
 
for_yanCommented:
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.
 
 

0
 
objectsCommented:
> 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
0
 
for_yanCommented:

The same thing with class Word,
"so that it wiil be expandable" :)

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 HashMapWithWordClass {

public HashMapWithWordClass() {

    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(m1.get(s) != null){
                        Word w = (Word) m1.get(s);
                        w.increment();
                        w.addLog(log);
                        m1.put(s,w);
                    }else
                    {
                        Word w = new Word(s, log);
                        m1.put(s,w);

                    }

                    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 + "  ");

            Word w = (Word) m1.get(s);

        //    Integer ii = (Integer)m1.get(s);
            System.out.print("" + w.getCount() + "  ");
           // ArrayList logg = (ArrayList)m2.get(s);
              ArrayList logg = w.getLogs();

            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 HashMapWithWordClass();
    }


}

class Word {
    String name;
           int count;
           ArrayList  fileNames;
public  Word(String name, String fileName){
    count = 1;
    this.name = name;
    ArrayList al = new ArrayList();
    al.add(fileName);
    this.fileNames = al;
}

public void addLog(String s){
    if(!fileNames.contains(s)) fileNames.add(s);

}

public void increment() {
    count++;
}

  public int getCount() {
      return count;
  }

    public ArrayList getLogs(){
        return fileNames;
    }

    
}

Open in new window



output:

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

0
 
vkchaitu82Author Commented:
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 .
0
 
for_yanCommented:
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).
0
 
objectsCommented:
> ==> 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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now