java file reading and storing it in a hashmap - java

I was trying to read a .txt file and wanted to store it in a hasmap(String, List). But when it tried to store the values were overwritten with the last value.
String filePath = "D:/liwc_new.txt";
HashMap<String,List<String>> map = new HashMap<String,List<String>>();
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String key = null;
List<String> value = new ArrayList<String>();
//putting words in key and cat strings in value of map
int count = 0;
String cats[]= null;
value.clear();
while ((line = reader.readLine()) != null)
{
String[] parts = line.split(":", 2);
value.clear();
count++;
key = parts[0].trim();
cats=parts[1].split(", ");
for(int i=0;i<cats.length;i++) {
cats[i]=cats[i].trim();
cats[i]=cats[i].replace("[", "");
cats[i]=cats[i].replace("]", "");
value.add(cats[i]);
}
map.put(key, value);
//map.put(key, value);
}

The line List<String> value = new ArrayList<String>(); should be moved to the first line of your while loop and both calls to clear removed.
The reason they are getting overwritten is you only ever allocate one list and put it in every value of the k:v pair of the map. So for every category, you have the same list, and the contents of that list are cleared and rebuilt every time a new line is read. So every value will have the contents of whatever was added after the last clear.
On the other hand, if you create a new list with each iteration, each category will have it's own list, what you want.

Delete the second occurrence of
value.clear();

Related

how to split the elements from csv file

if I have the following CSV file
how can I split the item name and the quantity and ignore the shop name, in a hashmap list?
This is what I have done so far:
public class ListRead {
HashMap<String, Integer> csvList = new HashMap<>();
public void loadManifest() throws IOException {
FileReader ListCSV = new FileReader("list.csv");
BufferedReader reader = new BufferedReader(ListCSV);
while (true) {
String line = reader.readLine();
if (line == null) {
break;
String[] listElement = line.split(",");
System.out.println(Arrays.toString(listElement));
// add to csvList
}
}
}
The output when I print listElement:
[>Shop A]
[shirt, 325]
[jeans, 475]
[>Shop B]
[car, 2]
[phone, 120]
[charger, 250]
If you want to ignore the shop name, then a simple parser will do:
Map<String, Integer> map = new HashMap<>();
String line;
String currentShop = null;
while ((line = reader.readLine()) != null) {
if (!line.matches(">.*,")) {
String[] listElement = line.split(",");
map.put(listElement[0], Integer.parseInt(listElement[1]));
}
}
The logic here is that if we encounter a shop line, indicated by > followed by a shop name and comma, then we don't try to parse that line into the map. Also, I assume that the separator for the data lines is really just commas, and no whitespace. If you expect whitespace, then you could split on something like \s*,\s* instead.
The code you've mentioned parses the line correctly. All you have to do now is insert those items into the created hashmap.
if(line.charAt(0)!='>')
{
int quantity = Integer.parseInt(listElement[1].trim());
String item = listElement[0].trim();
csvList.put( item , quantity);
}
Also, you might want to add another Map to store the shop names if required. The above code just ignores the shop information.

text file reading first line has key and second line onward value

****text file format:****
FirstName,lastname,role,startdate,emptype
sreedhar,reddy,Admin,20-2-2018,contract
shekar,kumar,Admin,20-2-2018,contract
RAJ,roy,Admin,20-2-2018,contract
somu,reddy,Admin,20-2-2018,contract
sumanth,reddy,Admin,20-2-2018,contract
Question:
How to read the text file and how to put in Map (Key ,vaule);
first line has key in map (ex: firstname,lastname,ect)
Second line on onwards value in map(eg:sreedhar,reddy,ect)
Map output:{Firstname:sreedhar,Lastname:reddy,role:Admin,startdat:2-6-2018}
please any one provide java code read the text file and put into map read has key, value pair.
You'll need to specify a different key for the Map as it requires a unique one each time:
A map cannot contain duplicate keys; each key can map to at most one
value.
So you're more than likely going to need a Map of Maps here:
Read in the file:
File file = new File("\\\\share\\path\\to\\file\\text.txt");
Add to scanner:
Scanner input = new Scanner(file);
Read the first line as your "header":
String[] headerArray = input.nextLine().split(",");
Create a Map of Maps:
Map<String, Map<String, String>> myMap = new HashMap<>();
Loop through the rest of the text file, adding to a Map, then adding that Map to the main Map, along with a key (I've used User0, User1...):
int pos = 0;
String user = "User";
while (input.hasNextLine()) {
Map<String, String> map = new HashMap<>();
int loop = 0;
String[] temp = input.nextLine().split(",");
for (String temp1 : temp) {
map.put(headerArray[loop], temp1);
loop++;
}
myMap.put(user + " " + pos, map);
pos++;
}
Once you break it down into steps, it makes life easier.
You can do something like this -
br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
String headerLine = line;
List<String> headerList = Arrays.asList(headerLine.split(","));
List<List<String>> valueListList = new ArrayList<List<String>>();
while (line != null) {
line = br.readLine();
String valueLine = line;
if(valueLine != null) {
List<String> valueList = Arrays.asList(valueLine.split(","));
valueListList.add(valueList);
}
}
Map<String, List<String>> map = new HashMap<String, List<String>>();
for(int i=0; i<headerList.size();i++){
List<String> tempList = new ArrayList<String>();
for(int j=0; j<headerList.size();j++){
tempList.add(valueListList.get(i).get(i));
}
map.put(headerList.get(i), tempList);
}
System.out.println(map);
Output:
{emptype=[contract, contract, contract, contract, contract],
startdate=[20-2-2018, 20-2-2018, 20-2-2018, 20-2-2018, 20-2-2018],
role=[Admin, Admin, Admin, Admin, Admin],
lastname=[kumar, kumar, kumar, kumar, kumar],
FirstName=[sreedhar, sreedhar, sreedhar, sreedhar, sreedhar]}

Read specific portion of file text, and then add them in array

I have a file text called messages.txt as following:
6107586533 44335557075557777
4848675309 53366 6699044404666804448
6107584096 94466602777330999666887770223377778077778 883 336687777
First column represents the numbers of recipient and second column represents the digit-coded message(and I will use a decoder method to decode the second column digits, which I have already created).I used the following method to read the file text:
public void readMessagesFromFile() throws Exception{
BufferedReader in = new BufferedReader(new FileReader("messages.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
String[] stringArr = list.toArray(new String[3]);
for (String x: stringArr) {
System.out.print(x);
}
}
However, it stores everything(as it should be) like the following:
6107586533 443355570755577774848675309 53366 66990444046668044486107584096 94466602777330999666887770223377778077778 883 336687777
How can I read and store only the following portion:
44335557075557777
53366 6699044404666804448
94466602777330999666887770223377778077778 883 336687777
I want to read and store as in lines,i.e. each row in the second column will be assigned to each index of the array), just want to exclude the first column when I store the second column.
This will split the line and keep everything after the first token
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
String[] temp = str.trim().split("\\s+",2);
list.add(temp[1]);
}
The call to trim is in case there are leading blanks.
As to the conversion to an array your code, you don't need it, you can iterate over the list.
for (String s : list)
{
System.out.println(s);
}

How to convert a csv to Hashmap if there are multiple values for a key ? (without using csv reader )

Here is the link which states the reading of data from csv to Hashmap.
Convert CSV values to a HashMap key value pairs in JAVA
However, I am trying to read a file of csv, in which there are multiple values for a given key.
Eg:
Key - Value
Fruit - Apple
Fruit -Strawberry
Fruit -Grapefruit
Vegetable -Potatoe
Vegetable -Celery
where , Fruit and Vegetable are the keys.
I am using an ArrayList<> to store the values.
The code I am writing is able to store the keys , but stores only the last corresponding value .
So, when I print the hashmap , what I get is : Fruit - [Grapefruit] Vegetable- [Celery]
How can I iterate through the loop, and store all the values?
Following is the code, which I have written :
public class CsvValueReader {
public static void main(String[] args) throws IOException {
Map<String, ArrayList<String>> mp=null;
try {
String csvFile = "test.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "";
StringTokenizer st = null;
mp= new HashMap<String, ArrayList<String>>();
int lineNumber = 0;
int tokenNumber = 0;
//read comma separated file line by line
while ((line = br.readLine()) != null) {
lineNumber++;
//use comma as token separator
st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
tokenNumber++;
String token_lhs=st.nextToken();
String token_rhs= st.nextToken();
ArrayList<String> arrVal = new ArrayList<String>();
arrVal.add(token_rhs);
mp.put(token_lhs,arrVal);
}
}
System.out.println("Final Hashmap is : "+mp);
} catch (Exception e) {
System.err.println("CSV file cannot be read : " + e);
}
}
}
Currently, you're putting a new ArrayList in your map for each value you find. This replaces the old list you had for that particular key. Instead, you should use the existing array list (if it is already there), and add your value to it.
You should therefore replace this:
ArrayList<String> arrVal = new ArrayList<String>();
arrVal.add(token_rhs);
mp.put(token_lhs,arrVal);
By this:
ArrayList<String> arrVal = mp.get(token_lhs);
if (arrVal == null) {
arrVal = new ArrayList<String>();
mp.put(token_lhs,arrVal);
}
arrVal.add(token_rhs);
you have:
while readline
while splitline
new ArrayList(); and list.add()
map.put(key, arraylist)
so everytime you executed the map.put(), a new arrayList would be put into the map, and the value of existing key would be overwritten with the new arraylist. You need first get the arrayList from the map, with certain key, and append the value to the arraylist. if key doesn't exist, create a new arrayList.
If you want to save that part of work, you could consider to use some MultiMap api, E.g guava ArrayListMultiMap
This is because you create a new arrVal list every time.
You should try this code
ArrayList<String> arrVal = mp.get(token_lhs);
if(arrVal == null) {
arrVal = new ArrayList<String>();
mp.put(token_lhs, arrVal);
}
arrVal.add(token_rhs);
It seems that you always initalize a new ArrayList inside your while (st.hasMoreTokens()) loop, so you will only have the last ArrayList used (containing only the last token of the csv line)

How to read a String (file) to array in java

Suppose there is a file named as SUN.txt
File contains : a,b,dd,ss,
I want to make dynamic array depending upon the number of attributes in file.
If ther is a char after comma then array will be of 0-4 i.e of length 5.
In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.
How do i do that?
Sundhas
You should think about
Reading the file into a String
Splitting the file by separator ','
Using a list for adding the characters and convert the list to an array, when the list is filled
As Markus said, you want to do something like this..
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine())!= null)
{
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String [] store = sb.toString().split(",");
I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.
If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.
If you would like a NULL you would have to add it in yourself at the end so you could do something like
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//Use an arraylist to store the values including nulls
ArrayList<String> store = new ArrayList<String>();
String line;
while((line = reader.readLine())!= null)
{
String [] splitLine = line.split(",");
for(String x : splitLine)
{
store.add(line);
}
//This tests to see if the last character of the line is , and will add a null into the array list
if(line.endsWith(","))
store.add(null);
}
String [] storeWithNull = store.toArray();
Well if you want want to simply open the file and store the content in a array of string then
1) open the file into a string
2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
but I'm curious why you can't use a String file directly ?
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
data.add(line.split(","));
line = readNextLine();
}
(assuming, your file contains 1..n lines of comma separated values)
You may want to have it like this:
"a,b,c,d," -> {"a", "b", "c", "d", null}
Here's a suggestion how to solve that problem:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
String[] values = new String[5];
String[] pieces = line.split(",");
for (int i = 0; i<pieces.length; i++)
values[i] = pieces[i];
data.add(values);
line = readNextLine();
}
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values
String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
while((line = br.readLine()) != null ){
StringTokenizer s = new StringTokenizer(line,",");
while (s.hasMoreTokens()){
value[row][col] = s.nextToken();
col++;
}
col = 0;
row++;
}
i havent tested this code
Read the file, using BufferedReader, one line at the time.
Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.
Load the String[] parts into a List.

Categories