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??

Member Avatar for hfx642

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

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

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

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

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

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.

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

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

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

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..

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?

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]);      
}
}
}
}
}

Query should be like: xxx filename1 yyy filename2 aaa bbb

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.

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>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.