Mahbubur Riad
Mahbubur Riad

Reputation: 17

convert string as key value pair and make it into one single line in java

I want to split string by colon and make it into key and value pairs. For that in convert function, I took a line and made it into key and value pairs.

I want to convert the line variable. The problem is in the convert function. It's not converting as expected. It must show as expected Output shown below. I don't want to split the code as a new line for every key-value pair. I want to do it by a comma in the same line.

Code is

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Splitter;

public class stack {

    public static Map<String, String> convert(String str, String SplitByWhat) {
        String[] tokens = str.split(SplitByWhat);
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < tokens.length - 1;) {
            map.put(tokens[i++], tokens[i++]);
        }
        return map;
    }

    public static void main(String[] args) {
        
        String line = "Name : Md . Abdur Rab , CI : Marketing  Petroleum Products , In : May 2013 , Hpc : President , Bd : April 14 , Bg : B + , Wd : October 10 , Sp : Quazi Manzida Rab , Bd : August 22 , Bg : B + , Ma : 689  Bara Moghbazar  Dhaka - 1217 , Tel : ( 0 ) 935 1684  835 0818 , Mob : 01819 247 932 , Email : [email protected]";
        
        Iterable<String> result = Splitter.on(" , ").trimResults().split(line);

        for (String v : result) {
            Map<String, String> response = convert(v, ":");
            System.out.println(response);
        }

    }
}

Output is

{Name = Md . Abdur Rab}
{CI = Marketing  Petroleum Products}
{In = May 2013}
{Hpc = President}
{Bd = April 14}
{Bg = B +}
{Wd = October 10}
{Sp = Quazi Manzida Rab}
{Bd = August 22}
{Bg = B +}
{Ma = 689  Bara Moghbazar  Dhaka - 1217}
{Tel = ( 0 ) 935 1684  835 0818}
{Mob = 01819 247 932}
{Email = [email protected]}

Expected Output

{Name = Md . Abdur Rab, CI = Marketing  Petroleum Products, In = May 2013, Hpc = President, Bd = April 14, Bg = B +, Wd = October 10, Sp = Quazi Manzida Rab, Bd = August 22, Bg = B +, Ma = 689  Bara Moghbazar  Dhaka - 1217, Tel = ( 0 ) 935 1684  835 0818, Mob = 01819 247 932, Email = [email protected]}

Upvotes: 0

Views: 1225

Answers (2)

Babyburger
Babyburger

Reputation: 1828

You are creating a new map each time you call convert() and printing that out, so you are printing a map for each line separated by ','.

What you want to do is something along the lines of:

public static void main(String[] args) {
    String line = ...
    Iterable<String> result = Splitter.on(" , ").trimResults().split(line);

    Map<String, String> response - new HashMap<>();
    for (String v : result) {
        Entry<String, String> entry = convert(v, ":");
        response.put(entry.getKey(), entry.getValue());
    }
    System.out.println(response);
}

where convert() returns type Entry<String, String>.

See: https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html

Upvotes: 1

verity
verity

Reputation: 385

Hello, I modified your code to print exactly what you wanted, so try this code:

public static Map<String, String> convert(String str, String SplitByWhat) {
    String[] tokens = str.split(SplitByWhat);
    Map<String, String> map = new HashMap<>();
    for (int i = 0; i < tokens.length - 1;) {
        map.put(tokens[i++], tokens[i++]);
    }
    return map;
}

public static void main(String[] args) {

    String line = "Name : Md . Abdur Rab , CI : Marketing  Petroleum Products , In : May 2013 , Hpc : President , Bd : April 14 , Bg : B + , Wd : October 10 , Sp : Quazi Manzida Rab , Bd : August 22 , Bg : B + , Ma : 689  Bara Moghbazar  Dhaka - 1217 , Tel : ( 0 ) 935 1684  835 0818 , Mob : 01819 247 932 , Email : [email protected]";

    Iterable<String> result = Splitter.on(" , ").trimResults().split(line);

    String response = "";

    for (String v : result) {
        response += convert(v, ":");
    }

    response = response.replace("{","");
    response = response.replace("}",", ");
    response = "{" + response + "}";

    System.out.println(response);

}

Have fun, I hope I could help you!

Upvotes: 0

Related Questions