Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-23-2008, 07:26 AM
Member
 
Join Date: Sep 2008
Posts: 5
Rep Power: 0
Anish is on a distinguished road
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!!!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-23-2008, 01:00 PM
Member
 
Join Date: Oct 2008
Location: UK
Posts: 45
Rep Power: 0
Paul Richards is on a distinguished road
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:

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:

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.

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("-----");
        }
    } 
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-23-2008, 02:14 PM
Member
 
Join Date: Oct 2008
Location: UK
Posts: 45
Rep Power: 0
Paul Richards is on a distinguished road
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:
File Type: zip 2D Data Store.zip (1.6 KB, 7 views)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-24-2008, 06:56 AM
Member
 
Join Date: Sep 2008
Posts: 5
Rep Power: 0
Anish is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting the number of columns in a 2D array, KalEl New To Java 9 10-21-2008 05:27 AM
Two diM aRRay and add rows and columns.... filly444 New To Java 2 08-30-2008 05:24 PM
SQL Query resultset into Excel Sheet chandpuri Database 6 06-05-2008 02:08 PM
Regarding retrieval of sheet names from a excel workbook yuvaraj23 New To Java 0 03-11-2008 02:20 PM
How to set the AutoFilter to Excel Sheet with Wither POI or JXL lnarayana_boga Advanced Java 0 01-29-2008 09:05 AM


All times are GMT +2. The time now is 01:56 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org