Results 1 to 4 of 4
  1. #1
    Anish is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default Counting Rows and Columns from Excel Sheet

    hi there,

    i'm trying to count the number of rows and columns that are in the excel sheet occupied by data. i have extracted the data and printed also.. but the array declaration is static... i want it dynamic.. coz there may be thousands of data and hence static doesnt work. my code is:

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

    public class Main {

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


    String [][] numbers = new String [6][2];

    File file = new File("E:\\anish.csv");

    BufferedReader bufRdr = new BufferedReader(new FileReader(file));

    String line = null;
    int row = 0;
    int col = 0;

    //read each line of text file

    while((line = bufRdr.readLine()) != null && row < 6)
    {
    StringTokenizer st = new StringTokenizer(line,",");
    while (st.hasMoreTokens())
    {
    //get next token and store it in the array

    numbers[row][col] = st.nextToken();
    col++;
    }
    col = 0;
    row++;

    }

    for (int i=0; i<numbers.length; i++)
    {
    for (int j=0; j<numbers[i].length; j++)
    {
    System.out.println(numbers[i][j]);
    }
    System.out.println("-----");
    }
    }
    }

    i know that there are 6 rows n 2 columns.. but every time the data will be changed.. so i need dynamic array declaration according to the number of rows and columns that are occupied by data.. so how can i do that???
    can anyone help me???

    thankx in advance!!!

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Anish,

    Arrays in Java are not extensible, they have a fixed size and are therefore not a good choice for something like this. My suggestion is that you need one of the collection classes, preferably something that implements the List interface. Typical examples are ArrayList or LinkedList; the latest documentation for the latter is at Java Platform SE 6.

    In fact, you will need (something like) a LinkedList of LinkedList objects, since your array is 2-dimensional. You would initialise it like this:

    Java Code:
    LinkedList<LinkedList<String>> numbers = new LinkedList<LinkedList<String>>();
    You would need to set each 'column' to a new LinkedList object. If it was me, I would probably develop my own class to store this data, since it is a bit cumbersome and the sort of thing you may want to re-use.

    Another point: what are you actually doing with the data? You call your variable numbers, but it in fact contains strings. If it does contain only numbers and you need to things with it that require it to be treated as a number (e.g. arithmetic), then you may be better off converting to a number and storing it as such. This means your array would contain int, double or float data. Your LinkedList would need to use the 'wrapper' classes: Integer, Double or Float.

    You can convert from String to Integer like this:

    Java Code:
    try {
        int number = Integer.parseInt(st.nextToken());
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
    }

    Finally, I have reposted your original code below, in a form that makes it easier for people to read.

    Java Code:
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class Main {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            String [][] numbers = new String [6][2];
            File file = new File("E:\\anish.csv");
            BufferedReader bufRdr = new BufferedReader(new FileReader(file));
            String line = null;
            int row = 0;
            int col = 0;
    
            //read each line of text file
            while((line = bufRdr.readLine()) != null && row < 6)
            {
                StringTokenizer st = new StringTokenizer(line,",");
                while (st.hasMoreTokens())
                {
                    //get next token and store it in the array
                    numbers[row][col] = st.nextToken();
                    col++;
                }
                col = 0;
                row++;
            }
    
            for (int i=0; i<numbers.length; i++)
            {
                for (int j=0; j<numbers[i].length; j++)
                {
                    System.out.println(numbers[i][j]);
                }
                System.out.println("-----");
            }
        } 
    }

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    I have had a further think about this, the detail really does depend on what you want to do. If you need to extract and print the data then you may find the attached files useful.

    However, if all you want to do is count, then you can do that without storing the data - you can just use your row and col variables.

    HTH,

    Paul
    Attached Files Attached Files

  4. #4
    Anish is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    thankx for ur reply...

    Actually I'm extracting the data from a *.csv (Comma Separated Values) and is read from Excel... The CSV files will be
    downloaded automatically to my system. Each and every seconds my system searches for CSV files (based on current date)...
    And if found, then the data begins to be extracted. I don't know how many data are present there. That's why I'm counting
    the number of rows and columns so that I can declare my Array Size dynamically using that counted number of rows and columns.
    And then further I've to change that data to the specified format that is pre-defined (format like..Date..mm/dd/yy to dd/mm/yy....
    Time Format-12 to 24..and many others..) in my system. And those formated data are then stored in .XLS format or may
    be again to .CSV format. SO I've to first determin the number of columns and rows. That's why I'm counting rows and columns.
    Well.. if Array is sufficient for that.. then how can I use that? Those data may be int..float...or String.

    Thankx in Advance...

Similar Threads

  1. SQL Query resultset into Excel Sheet
    By chandpuri in forum JDBC
    Replies: 8
    Last Post: 01-24-2011, 07:53 PM
  2. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 06:27 AM
  3. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 06:24 PM
  4. Replies: 0
    Last Post: 03-11-2008, 03:20 PM
  5. How to set the AutoFilter to Excel Sheet with Wither POI or JXL
    By lnarayana_boga in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •