I have one CSV file containing so many attribute but i want max and min values of particular specified columns.
I have write following code for finding minimum value from each specified column,but i got only minimum of last column, not all column.

In dataset.csv file contains

If give identifier=3 and location are 0,1,3....so answer will be 12,-189,-78 for minimum.

So please help me to find solution for it.

public static void main(String args[])throws IOException

    int q,i,loc[];

    DataInputStream din = new DataInputStream(System.in);

    System.out.println("Enter No. of Idenifier =");
    loc= new int[q];

    System.out.println("Enter "+i+"th identifier location :");

    BufferedReader CSVFile = new BufferedReader(new FileReader("dataset.csv"));

    String dataRow = CSVFile.readLine(); // Read first line.

    int min[]=new int[q],max[]=new int[q];
    int dataIndex = 0;

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

       for (String item:dataArray) {
         System.out.print("no records found..." + "\t");
        else{ continue;}    

    String aspStr = null;
    for(int j=0;j<q;j++)
        aspStr += dataArray[loc[j]]+",";//+dataArray[loc[1]]+","+dataArray[loc[2]];

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

    System.out.println("min0 min1 min2="+min[0]+" "+min[1]+" "+min[2]);

So above code give only correct output for min[2] only.

thank you.

Recommended Answers

I find your code confusing. Perhaps your naming conventions are too cryptic.

You also only provide part of the code, not the whole class.

I'm also uncertain what it is meant to do. Can you clarify please?

Jump to Post

All 3 Replies

I find your code confusing. Perhaps your naming conventions are too cryptic.

You also only provide part of the code, not the whole class.

I'm also uncertain what it is meant to do. Can you clarify please?

Can you make a complete program that compiles, executes and shows the problem?

Also please post the output from the program that shows the problem.

Have you tried debugging the code by adding some println statements to show the values of variables as they are changed and looked at? You need to see what the comuter sees so you can understand what the code is doing.

Ok, given assumptions as...

1) A user enters a correct identifier number.
2) The same user enters correct column numbers needed to find the minimum value in the data.
3) The csv file contains correct data format & number of identifier.

Lines 13~17, you are asking a user to enter the location. How could you choose only 3 locations out of 4? You seems to keep asking 4 times... I am guessing that you would enter the same number of location. If so, that is one of the problem in your code.

// if you enter 0, 1, 3, 3
// the loc[] array will be [0, 1, 3, 3]
// if you enter 1, 3, 0, 0
// the loc[] array will be [1, 3, 0, 0]

Lines 30~34 are useless to validate the line data. What you need to do is to check whether the length of the split array is equal or greater than the highest number of location given by the user. If it is shorter, display the error message and skip the line to the next.

I'm surprised that you assigned null to aspStr in line 36 but then attempt to add it with a string in line 39 and it goes through the compiler? Also, this line is pretty much useless...

Lines 40~47, supposed, min[j] is equal to 0 (from new int[q]), there are 2 problems here. First problem, your if is useless. Instead of having both if and else, just set the if condition to check if min[j] is greater than the reading data column. Second problem which is even more important, the min[j] will never be lower if the minimum value of the column is greater than 0 (as you see the first column minimum is 12). Why? Because you initialize it and it would be 0. You need to initialize it to maximum value of its data type which is Integer.MAX_VALUE. You would do the similar way to max array but use Integer.MIN_VALUE instead. Go back to fix it after line 23.

int min[]=new int[q],max[]=new int[q];
for (int i=0; i<q; i++) {
  min[i] = Integer.MAX_VALUE;  // set maximum value to each min
  max[i] = Integer.MIN_VALUE;  // set minimum value to each max

Lines 37~48, the loop is incorrect because you are going through each column and use the column data as if it is the user-selected column. This is why I said that your user input for selected column will cause problem. How to solve this depends on how you store the user input. You need to come up with a way to deal with user input first.

Be a part of the DaniWeb community

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