954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read map values from .CSV

Hi all,

import java.io.*;
import java.util.*;
class MapDemo{
public static void main(String args[]){
    Map<Integer, Object[]> tMap = new TreeMap<Integer, Object[]>();
    Object[] values = new String[] {"Tree", "Map"};
    tMap.put(1, values);
System.out.println("Keys of tree map: " + tMap.keySet());
System.out.println("Values of tree map: " +  Arrays.deepToString(tMap.values().toArray()));
}
}


The above code works good to print the array of String values. But, the issue is, I need to read the keys and values from a .CSV file by just giving the name of the file.

Any ideas??

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Use a JFileChoose to select the file.
Read in lines of data.
Use tokenize to process each line to get your data values.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Or pass the name of the file in the args to the main method, then continue as above.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Any other way.?bcoz, i dint understand about JFileChooser..
I just want to implement in core java, i dont have touch in advanced..:(

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

pass the name of the file in the args to the main method using the command line

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I have written code to parse the input string already, and read the file using InputStream.
Now, how to use that in Map?

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
how to use that in Map?


What is your key and what is your value?
Read the API doc for more information about how a Map works. http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/

Find Map in the lower left, click on it and the doc is shown in the main window.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Key is integer and the value is String array.
want to read the content in the .CSV file into map

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Have you tried coding a simple program with a Map, a String array and an int to see how to use the Map?

http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Yes, I have..It worked.
that code was there in my first post.

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Then I don't understand what your problem is.
Can you explain?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

When the input string is parsed and the file(.CSV) name is encountered, it should automatically read the contents of the file into the Map. We must not give the key, string array manually as in the first post. The input is only a query which contains the file name. I have written the code using StringTokenizer to parse the input query. I need to read the file into the Map..

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
I need to read the file into the Map..


What data structure/object are you using to store the data that you read in from the file?
Do the job in several steps:
1) read and parse the data into objects
2) save the objects in a collection
3) save the collection in a Map

If these are the steps you need to do, which steps are you having problems with?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Here is my code:

import java.util.StringTokenizer;
import java.io.*;
class StringTokenizerDemo{
public static void main(String args[]) throws java.io.IOException {
System.out.println("Type the Query and press 'Enter.'");
String string = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
string = reader.readLine(); 
System.out.println("You typed: " + string); 
StringTokenizer st = new StringTokenizer(string);
while (st.hasMoreTokens()) {
st.countTokens(); //COUNTS THE NUMBER OF TOKENS REMAINED IN THE STRING
st.nextToken();
if(st.countTokens()==5 || st.countTokens()==3){ 
String fName=st.nextToken()+".csv";
System.out.println(fName);
String thisLine;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
while ((thisLine = myInput.readLine()) != null)
{ 
String[] result = thisLine.split(",");
for (int x=0; x<result.length; x++)
 System.out.println(result[x]);      
}
}
}
}
}
javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Query should be like: xxx filename1 yyy filename2 aaa bbb

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Do you really not indent your code?
Or was there a problem when you posted it?
Unindented code like you have posted is harder to read and understand.

What is the purpose of the code you posted?

Can you copy and paste here the command prompt's console from when you execute your code.
It should show the input to the program and all the program's printed output.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

It just parse the query and read the first .csv file, print the content in it on the screen.

C:\Java>javac StringTokenizerDemo.java
Note: StringTokenizerDemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

C:\Java>java StringTokenizerDemo
Type the Query and press 'Enter.'
join file1 with file2 on id
You typed: join file1 with file2 on id
file1.csv
id
firstName
lastName
1
aaa
bbb
2
xxx
yyy
file2.csv
id
place
1
place1
2
place2

C:\Java>

javausers
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Did the program do what you wanted?
If not you need to add some comments to its output describing what is wrong.

When you print out the values of variables, you should include the name of the variable in the printout:
fName=file1.csv

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: