I am working with CSVfile program.
I have already calculate something from it.

Name,Age,Sex,Pincode,Disease
1. aaa,4,M,35265,flu
2. bbb,4,M,35652,Gastric
3. eee,6,F,35265,Brain
4. sss,7,M,32564,flu

I have made program for calculating no. of age,sex and pincode as attached in the code.
In this i have calculated that age=4 comes how many times and also how many distinct ages are there ion the file.
Also Original file has so many records , nearly 100 records(Rows).

Now i am stuck with one problem.
I just want to find that there are how many rows having age=4 and sex=M or sex=M and pincode=35265 or age=4 and Pincode=35265

Name and disease is not required in the whole processing.It is left as it is.There is no need to compare name and disease.

Only particular elements of one row is compared with the same elements of the other row.

It always execute else method and gives output "Something Wrong"...

i also attached my code with this.

import java.io.*;
import java.lang.*;
import java.util.Arrays;

public class CSVRead{

int x;
int y;
public static void main(String[] args) throws IOException {

System.out.println("Enter the value of K=");
DataInputStream din = new DataInputStream(System.in);
String n = din.readLine();
int k = Integer.parseInt(n);
System.out.println("\n"+k);

  BufferedReader CSVFile = 
        new BufferedReader(new FileReader("C:/Users/JAL/Desktop/dataset.csv"));

  String dataRow = CSVFile.readLine(); // Read first line.
  // The while checks to see if the data is null. If 
  // it is, we've hit the end of the file. If not, 
  // process the data.

int j=0,i=0,z1=0;  
int p=0,z=0,k1=0,age=0;
int pp=0;
int a[][] = new int[100][1];
int s1[] = new int [2];
int p2[][] = new int[120][2];
int as[][] = new int[120][2];
int se[] = new int[120];

for (int s = 0 ; s < p2.length; s++)
{
    p2[s][0] = 0;
    p2[s][1] = 0;
    as[s][0]=0;
    as[s][1]=0;
}

for (int s = 0 ; s < 100; s++)
{
    a[s][0] = 0;
}

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

   for (String item:dataArray) {
    if(item.length()==0)
     System.out.print("?" + "\t");
    else{}
      //System.out.print(dataArray[1] + "\t"); 

   }
   //System.out.println(dataArray[1]); // Print the data line.
//-------------------------------------------------------------------------------------------------------------------------------------------
    String v = dataArray[1];    //For calculation of particular age
    int x = Integer.parseInt(v);
    a[x][0] = a[x][0] + 1;
    int x4 = x;

//-------------------------------------------------------------------------------------------------------------------------------------------
    String v1 = dataArray[2];
    if(v1.equals("male"))
    {
    s1[0] = s1[0] + 1;
    //se[pp] = 0;
    }
    else if(v1.equals("female"))
    {
    s1[1] = s1[1] + 1;
    //se[pp] = 1;
    }
    else
    { System.out.println("Something wrong");}
    //pp++;
//-------------------------------------------------------------------------------------------------------------------------------------------
    String v2 = dataArray[3];
    int x1 = Integer.parseInt(v2);
    z1 = 0;
    for(int s=0;s<p2.length;s++)
    {
        if( p2[s][0]==x1 )
        {       
            p2[s][1] = p2[s][1]+1;
        }
        else
        {
            z1++;   
        }       
    }
    if (z1 == p2.length)
    {
        p2[z][0] = x1 ;
        p2[z][1]=p2[z][1]+1;
    }
    z++;
//------------------------------------------------------------------------------------------------------------------------------------------

    for(int s=0;s<a.length;s++)
    {
        if(v1.equals("male") && a[s][0]==x)
        {       
            as[s][0] = a[s][0];
            as[s][1] = as[s][1]+1;
        }
        else if(a[s][0]==x4  && v1.equals("female") )
        {       
            as[s][0] = a[s][0];
            as[s][1] = as[s][1]+1;
        }
        else
        { 
            System.out.println("Something wrong");
        }   
    }

//------------------------------------------------------------------------------------------------------------------------------------------
    dataRow = CSVFile.readLine();// Read next line of data. 
}

 // Close the file once all data has been read.
  CSVFile.close();

  // End the printout with a blank line.
  System.out.println();

 } //main()
} // CSVRead

So please help me to get desired output.

Thank you..

Recommended Answers

All 5 Replies

It always execute else method and gives output "Something Wrong"...

Add some more data to the printed message to describe what the problem is and give the values of all the variables being used in the if statements. The printed output should help you solve the problem.

dear NormR1,

i have one CSV file contains 100 records of comma separated values.
This file containing following data similar data.

aaa,4,M,35265,flu
bbb,4,M,35652,Gastric
eee,4,F,35265,Brain
sss,7,M,32564,flu
ada,9,M,32561,Brain
afa,9,F,35261,flu
aga,9,M,32561,Gastric

i just want to compare 4 M and next line 4 M with all other lines to get similar things
similarly from 1st line M 35265 and next line M 35265 with all other lines to get similar records

at last i want to find
4 M 35265 and similar record froma all data if available.

hope now you will get it.

Thanking you
please help me as soon as possible.

Did you try debugging your code by adding more information to the println statement that prints out the error message so that you know why there was an error?

yes i have tried it.
it always go into the loop of else part as

System.out.println("Something wrong"); at line 117.

it have to go to the if loop not else loop.

so please tell me modififcation in for or if loop.

What is printed out that tells you why the code executed the statement at line 117? What are the values of v1, x, x4, s and a[s][0]?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.