We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,629 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Why HashMap doesn't give correct output?

I have made one program that work on hash map.
I want to do one thing.
All the tuples(row) with defined quasi identifier location having value >=k will be moved from map m1 to asMap and it also removed from m1.
So at last i am printinf the output of each map.but it doesn't give me correct output.
So please tell me where the mistake is done?
In new_dataset.csv file contains the following value
12,-8000,251,36,456
14,5000,215,22,741
2,16520,45,841,65
32,1200,361,32,45
My code is as follows

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

public class csv {

    public static void main(String args[]) throws IOException
    {
        Map <Integer,String[]> m1 = new HashMap<Integer,String[]>();
        Map<Integer,String[]> asMap = new HashMap<Integer,String[]>();
        /*======================================================================*/
        int q,i1,loc[],min[],max[],len[];

DataInputStream din = new DataInputStream(System.in);


System.out.println("Enter No. of Quasi Idenifier =");
q=Integer.parseInt(din.readLine());
loc= new int[q];
min=new int[q];
max=new int[q];
len=new int[q];
//flagp=new int[q];



for(i1=0;i1<q;i1++)
{
System.out.println("Enter "+i1+"th Quasi identifier location :");
loc[i1]=Integer.parseInt(din.readLine());
min[i1]=Integer.MAX_VALUE;
max[i1]=Integer.MIN_VALUE;
len[i1]=0;
}

System.out.println("Enter the value of K=");
String n = din.readLine();
int k = Integer.parseInt(n);
BufferedReader CSVFile = new BufferedReader(new FileReader("new_dataset.csv"));
        String dataRow = CSVFile.readLine();
        int count=1;

        while(dataRow!=null)
        {
            String dataArray[] = dataRow.split(",");
            m1.put(count, dataArray);

            count++;
            dataRow=CSVFile.readLine();
        }
        int a=m1.size();
        //System.out.println(a);
        for(int j = 1; j<=a; j++)
        {
        String[] aa1=m1.get(j);
        for(int p=0;p<q;p++)
        System.out.print(aa1[loc[p]]+"\t");
for(int p=0;p<q;p++)
        {
        if( min[p] > Integer.parseInt(aa1[loc[p]]))
        {
            min[p] = Integer.parseInt(aa1[loc[p]]);
        }
        if( max[p] < Integer.parseInt(aa1[loc[p]]))
        {
            max[p] = Integer.parseInt(aa1[loc[p]]);
        }
        }
 System.out.println();
        }
        for(int p=0;p<q;p++)
        {
            if(min[p]<0)
            {
            len[p]=Integer.toString(min[p]).length()-1;
            if(len[p]>Integer.toString(max[p]).length())
                len[p]=Integer.toString(min[p]).length();
            }
         else
            {
        len[p]=Integer.toString(min[p]).length();
            if(len[p]>Integer.toString(max[p]).length())
                len[p]=Integer.toString(min[p]).length();
            }
        System.out.println(p+"min "+min[p]);
        System.out.println(p+"max "+max[p]);
        System.out.println(p+"len "+len[p]);
        }
int am[]=new int[a];
        int row[]=new int[a];
        int remove[] = new int[a];
        int flagp[] = new int[a];
        int f1[] = new int[a];
        for(int t=0;t<a;t++){am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0;}

        for(int j=1;j<=a;j++)
        {
          int cnt1=0;
          if(am[j-1]==1)
          {
              break;
          }
          else
          {
          for(int p=j+1;p<=a;p++)
          {


              //System.out.println("hello");
              String[] aa2 = m1.get(j);
              String[] aa3 = m1.get(p);
              //System.out.println("Hello "+aa2[loc[1]]+" and "+aa3[loc[1]]);
              int h=0;
              int cnt=0;
              while(h<q)
              {
                  //System.out.println("inside while "+h);
              if(aa2[loc[h]].equals(aa3[loc[h]]))
              {
                  cnt++;
              }
              h++;
              }
              if(cnt==q)
              {
                  remove[j-1]=1;
                  remove[p-1]=1;
                  cnt1++;
                  am[p-1]=1;
                  //System.out.println("Both are equal at "+j+" "+p+" "+" count ="+cnt);

                  row[j-1]=cnt1+1;
                  //System.out.println("row "+row[j-1]+" ="+cnt1);
              }


              }
              System.out.println("value of row "+j+" = "+row[j-1]);

             if(row[j-1]>=k)
                  {
                        for(int p1 = 0;p1<a;p1++)
                        {
                            if(remove[p1]==1 && flagp[p1]==0)
                            {
                                flagp[p1]=1;
                                System.out.println("remove= "+(p1+1));
                                f1[p1]=p1+1;
                                asMap.put(p1+1,m1.get(p1+1));
                                //m1.remove(p1+1);

                            }
                        }
                        //m1.remove(2);
                  }
          }
        }

        for(int jj=0;jj<f1.length;jj++)
        {
            if(f1[jj]!=0)
                m1.remove(f1[jj]);
            //System.out.println("The Value is = "+f1[jj]);
        }
for(Map.Entry<Integer, String[]> mapEntry : m1.entrySet())
        {
        int key = mapEntry.getKey();
        String[] aa1=mapEntry.getValue();
        System.out.print("Key=> "+key);
        for(int p=0;p<aa1.length;p++)
        {
        System.out.print("\t"+aa1[p]+"\t");
        }
        System.out.println();
        }
System.out.println("==============================================================");
//=========================================================================================
for(Map.Entry<Integer, String[]> mapEntry : asMap.entrySet())
        {
        int key = mapEntry.getKey();
        String[] aa1=mapEntry.getValue();
        System.out.print("Key=> "+key);
        for(int p=0;p<aa1.length;p++)
        {
        System.out.print("\t"+aa1[p]+"\t");
        }
        System.out.println();
        }
}
}

Now i have printed bot the map.but it doesn't give correct output.
I want all the rows who satisfies condition >=k into asMap, but it is not working properly.Also i am checking the row with same Quasi identifie.

thank you.

2
Contributors
6
Replies
3 Days
Discussion Span
9 Months Ago
Last Updated
7
Views
Question
Answered
jalpesh_007
Posting Whiz
350 posts since Sep 2010
Reputation Points: 4
Solved Threads: 37
Skill Endorsements: 5

Could you rewrite the code so that it includes the input it is reading?
For example replace the DataInputStream class with a Scanner class and create the Scanner object with a String instead of System.in
= new Scanner("the input for the class here\nwith lines\nand more lines\n");

That will make it much easier for anyone to test (no need to type input) and will make sure every one tests with the same data.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

NormR1:
As you have said, i have updated my program.
But still i got the same problem.
Here i am attaching updated code.
Please let me know where is the problem.
As i want output in m1 with only distinct tuples and in asMap the repeated tuple.

Thank you.

import java.io.*;
import java.util.*;
//import java.math.*;
/**
 *
 * @author JAL
 */
public class csv {

    public static void main(String args[]) throws IOException
    {
        Map <Integer,String[]> m1 = new HashMap<Integer,String[]>();
        Map<Integer,String[]> asMap = new HashMap<Integer,String[]>();
        /*======================================================================*/
        int q,i1,loc[],min[],max[],len[];
        Scanner input= new Scanner(System.in);
        System.out.println("Enter No. of Quasi Idenifier =");
        q=input.nextInt();
loc= new int[q];
min=new int[q];
max=new int[q];
len=new int[q];
for(i1=0;i1<q;i1++)
{
System.out.println("Enter "+i1+"th Quasi identifier location :");
loc[i1]=input.nextInt();
min[i1]=Integer.MAX_VALUE;
max[i1]=Integer.MIN_VALUE;
len[i1]=0;
}

System.out.println("Enter the value of K=");
int k = input.nextInt();
BufferedReader CSVFile = new BufferedReader(new FileReader("new_dataset.csv"));
        String dataRow = CSVFile.readLine();
        int count=1;

        while(dataRow!=null)
        {
            String dataArray[] = dataRow.split(",");
            m1.put(count, dataArray);

            count++;
            dataRow=CSVFile.readLine();
        }
        int a=m1.size();
        for(int j = 1; j<=a; j++)
        {
        String[] aa1=m1.get(j);
        for(int p=0;p<q;p++)
        System.out.print(aa1[loc[p]]+"\t");
        for(int p=0;p<q;p++)
        {
        if( min[p] > Integer.parseInt(aa1[loc[p]]))
        {
            min[p] = Integer.parseInt(aa1[loc[p]]);
        }
        if( max[p] < Integer.parseInt(aa1[loc[p]]))
        {
            max[p] = Integer.parseInt(aa1[loc[p]]);
        }
        }
        System.out.println();
        }
        for(int p=0;p<q;p++)
        {
            if(min[p]<0)
            {
            len[p]=Integer.toString(min[p]).length()-1;
            if(len[p]>Integer.toString(max[p]).length())
                len[p]=Integer.toString(min[p]).length();
            }
         else
            {
        len[p]=Integer.toString(min[p]).length();
            if(len[p]>Integer.toString(max[p]).length())
                len[p]=Integer.toString(min[p]).length();
            }
        System.out.println(p+"min "+min[p]);
        System.out.println(p+"max "+max[p]);
        System.out.println(p+"len "+len[p]);
        }
        int am[]=new int[a];
        int row[]=new int[a];
        int remove[] = new int[a];
        int flagp[] = new int[a];
        int f1[] = new int[a];
        for(int t=0;t<a;t++){am[t]=0; row[t]=0;remove[t]=0;flagp[t]=0;f1[t]=0;}

        for(int j=1;j<=a;j++)
        {
          int cnt1=0;
          if(am[j-1]==1)
          {
              break;
          }
          else
          {
          for(int p=j+1;p<=a;p++)
          {
           String[] aa2 = m1.get(j);
              String[] aa3 = m1.get(p);
              int h=0;
              int cnt=0;
              while(h<q)
              {
                      if(aa2[loc[h]].equals(aa3[loc[h]]))
              {
                  cnt++;
              }
              h++;
              }
              if(cnt==q)
              {
                  remove[j-1]=1;
                  remove[p-1]=1;
                  cnt1++;
                  am[p-1]=1;
                  row[j-1]=cnt1+1;

              }


              }
              System.out.println("value of row "+j+" = "+row[j-1]);

             if(row[j-1]>=k)
                  {
                        for(int p1 = 0;p1<a;p1++)
                        {
                            if(remove[p1]==1 && flagp[p1]==0)
                            {
                                flagp[p1]=1;
                                System.out.println("remove= "+(p1+1));
                                f1[p1]=p1+1;
                                asMap.put(p1+1,m1.get(p1+1));


                            }
                        }

                  }
          }
        }

        for(int jj=0;jj<f1.length;jj++)
        {
            if(f1[jj]!=0)
                m1.remove(f1[jj]);

        }
        for(Map.Entry<Integer, String[]> mapEntry : m1.entrySet())
        {
        int key = mapEntry.getKey();
        String[] aa1=mapEntry.getValue();
        System.out.print("Key=> "+key);
        for(int p=0;p<aa1.length;p++)
        {
        System.out.print("\t"+aa1[p]+"\t");
        }
        System.out.println();
        }
System.out.println("==============================================================");
//=========================================================================================
for(Map.Entry<Integer, String[]> mapEntry : asMap.entrySet())
        {
        int key = mapEntry.getKey();
        String[] aa1=mapEntry.getValue();
        System.out.print("Key=> "+key);
        for(int p=0;p<aa1.length;p++)
        {
        System.out.print("\t"+aa1[p]+"\t");
        }
        System.out.println();
        }

}
}
jalpesh_007
Posting Whiz
350 posts since Sep 2010
Reputation Points: 4
Solved Threads: 37
Skill Endorsements: 5

Here also i m giving the data contained by new_dataset.csv file.
It contains following data.

grt,-8000,male,36021,3,flu
abcd,4,male,35624,1,heart disease
ser,5,male,36025,2,cancer
swr,5,male,35021,3,flu
gut,5,female,36020,1,flu
gut,5,male,36020,4,Ulcer
fads,6,male,36029,5,cancer
fut,6,male,36020,6,flu
fut,7,female,35026,8,heart disease
hut,7,male,35026,2,Ulcer
tryd,7,male,35652,3,cancer
acx,7,male,35026,4,flu
admc,7,female,35654,6,flu
grt,600,female,36020,3,flu
der,3256,female,35654,3,flu
lkjdn,3256,female,35654,3,heart disease
asdh,16542,male,36021,2,flu
asbc,16542,male,36021,4,cancer

i have take(It only accepts numeric data)
no.of quasi identifier=2
location are 1,3
k=2
for running above example.

jalpesh_007
Posting Whiz
350 posts since Sep 2010
Reputation Points: 4
Solved Threads: 37
Skill Endorsements: 5

It looks like the program still asks the user for input. Can you change it so that the program contains all the data it needs and does not require uer input. See my last post for how to do it.

Have you tried debugging the code by adding printlns that show the values of variables as they are changed and ued by the code. That is what I'll have to do to find the problem. When I do find the problem, I'll still ask you to add printlns until you see the problem. You need to learn how todebug your code.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

NormR1:
As you said, i have printed values of all the variables.

And found problem that my loop of "j" (at line 90) is not going after 6 instead of it has to go upto 20.

so now please tell me where the actual problem is.

jalpesh_007
Posting Whiz
350 posts since Sep 2010
Reputation Points: 4
Solved Threads: 37
Skill Endorsements: 5

I am not able to test the code because it does not include all the data it needs to execute. There are prompts made for input that should not be made but should be answered by having values in the the code. I showed you how to define the Scanner object so that it has the answers to all the prompts in it. See my previous post.

If you see what the problem with the code is:

"j" (at line 90) is not going after 6 instead of it has to go upto 20.

then you need to examine the code to see why that is happening. Add more printlns to show why the code is doing what it is doing.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Question Answered as of 9 Months Ago by NormR1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0784 seconds using 2.8MB