Results 1 to 15 of 15
- 02-09-2009, 11:00 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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.
- 02-09-2009, 11:14 PM #2
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-09-2009 at 11:15 PM. Reason: missing preposition
- 02-09-2009, 11:20 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
Thanks...
However, it would be gr8 if u can tell me bout which package to use for my problem
- 02-10-2009, 12:14 AM #4
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!
- 02-10-2009, 12:35 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
Thanks for the direction. I think this will help me a lot to get the things started..
- 02-11-2009, 08:50 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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
- 02-11-2009, 09:42 PM #7
I don't get it...
Like Steve said...the user when enters a zipcode, the program should select a specific .csv file related to the zipcode entered
key = 12345, value = "Florida.csv"
key = 98765, value = "Texas.csv"
I'm I missing something?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-11-2009, 09:59 PM #8
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0
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 ?
- 02-11-2009, 11:13 PM #9
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...
- 02-12-2009, 06:52 AM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-12-2009, 09:06 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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"
- 02-12-2009, 09:29 PM #12
importing
Try adding the following:
Luck,Java Code:import java.util.StringTokenizer;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-13-2009, 08:01 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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...
- 02-13-2009, 08:36 PM #14
give it a twirl...
Give it a try... see which method works the best for you.
String (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-13-2009, 09:07 PM #15
Similar Threads
-
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
how to search xml file data based on the given keyword from html form?
By nicemothi in forum XMLReplies: 0Last Post: 04-04-2008, 09:36 AM -
Reading Data from a file
By ramachandran in forum New To JavaReplies: 2Last Post: 10-24-2007, 07:22 AM -
Reading file data that contains no spaces
By jdepue in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks