Results 1 to 15 of 15
  1. #1
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Reading data from csv file based on specific input

    Hi.

    I have a project in which, the user when enters a zipcode, the program should select a specific .csv file related to the zipcode entered (for eg: if the zipcode for florida is entered, the program should read the data from florida.csv file).
    Once, the file is found, the program should execute certain simple calculations based on the data in the file. The final output will be the result of these calculations.

    I am initially stuck up with the first part, which is selection of a specific .csv file based on the zipcode entered by the user.

    So, can u pls help me out for the same. If there is any ready code available for it, pls post it. Its really urgent.

    Hoping for kind co-operation and thanks in advance.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    We normally avoid telling people what to code; our goal is to answer specific questions about the code you have written. However, here's a hint:

    Based on the zip code the user input, you want to *Map* the zip code to a file, such as FL.csv.

    Here's the link to the API.
    Last edited by Steve11235; 02-10-2009 at 12:15 AM. Reason: missing preposition

  3. #3
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    Thanks...
    However, it would be gr8 if u can tell me bout which package to use for my problem

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK, OK. Follow the link. Look at the Map interface, to see how the concept works. Then, look at Hashmap. Then, you have to write some code!

  5. #5
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    Thanks for the direction. I think this will help me a lot to get the things started..

  6. #6
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    Well, I went through map. It shows dat map is useful to only for mapping keys to at the most only 1 value. But, in my project, i ned to match the user input zipcode with the stored values of rainfall, ETo

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I don't get it...
    the user when enters a zipcode, the program should select a specific .csv file related to the zipcode entered
    Like Steve said...
    key = 12345, value = "Florida.csv"
    key = 98765, value = "Texas.csv"

    I'm I missing something?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    OK I got similar question i need to read and then rewrite the csv values. So there isn't special class for that, best way should be reading all line by line and then separate values ?

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    The File class will allow you to access files on the computer.

    BTW, Java can use slash as the file separator for Windows, so you don't need to use backslash. For example, "c:/temp/FL.csv".

    Input and output is performed using "streams" for bytes and "readers" and "writers" for character-based data. You want to use readers and writers. These classes are layered, so that the high-function classes require a lower-function class to be created first.

    Look at FileReader (lower-function class) and BufferedReader (higher function class) that will allow you to read a line at a time from your file.

    Now, you can tie zip codes to file names, and you can read the information in the file. I'd like to see some code that does those two tasks before we keep going...

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The simplest way to read them would be with a LineNumberReader, which can be constructed from a FileReader, which can be constructed from a File, which can be constructed from a String, such as "Texas.csv".

    This will read a full line at a time. You then need to parse the line based on commas, which can be done easily with the String.split method.

    ..hope that helps you get started.

  11. #11
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    Well I have the following code, which reads data from .csv file and then presents it as output (without doing any processing whatsoever).

    //class to read CSV file :

    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;


    public class readCSV

    { //readCSV class starts here

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

    { //main method starts

    String fName = "test1.csv";//csv file which u wanna read

    String thisLine; //string variable to take each record at a time

    int count=0;

    FileInputStream fis = new FileInputStream(fName);
    //A FileInputStream obtains input from a file

    DataInputStream myInput = new DataInputStream(fis);
    /*data input stream lets an application read primitive Java data types
    from an underlying input stream*/

    while ((thisLine = myInput.readLine()) != null)
    { //beginning of outer while loop
    StringTokenizer st =new StringTokenizer(thisLine,",");
    while(st.hasMoreElements()){
    String field = st.nextToken();
    System.out.print(field+", ");
    }
    System.out.println();

    }
    }
    }

    But, it shows following error:-

    C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\irrigation\readCSV.java:41: cannot find symbol
    symbol : class StringTokenizer
    location: class readCSV
    StringTokenizer st =new StringTokenizer(thisLine,",");
    ^
    C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\irrigation\readCSV.java:41: cannot find symbol
    symbol : class StringTokenizer
    location: class readCSV
    StringTokenizer st =new StringTokenizer(thisLine,",");
    ^
    I have attached test1.csv zip which contains test1.csv file in it.

    Instead of using "reader" and "writer" I have try to use "stream"
    Attached Files Attached Files

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default importing

    Try adding the following:
    Java Code:
    import java.util.StringTokenizer;
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    Oh...i got the problem ..i din't import the util ..
    now its working..

    but, i also wanted to know that is it good to use stringtokenizer or can i also sue split method of string class...

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default give it a twirl...

    Give it a try... see which method works the best for you.

    String (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Yes, both will work. One is not necessarily better than the other.

    When using "," as your separator, watch out for situations were character data contains a ",". Neither method will handle that correctly.

    I use tabs and no character delimiter to avoid the problem.

Similar Threads

  1. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 10:26 AM
  2. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM
  3. Replies: 0
    Last Post: 04-04-2008, 10:36 AM
  4. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 08:22 AM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 05:58 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
  •