Sunteți pe pagina 1din 12

Input:- The input is passed as string in the Program

"MSH|^~&|AccMgr|1|||20050110045504||ADT^A08|599102|P|2.3|abc|bui|mko|002|8duy|AsCi
i|hl7|\r"

+"EVN|A01|20050110045502||||\r"
+"PID|1||10006579^^^1^MRN^1||DUCK^DONALD^D||19241010|M||1|111 DUCK
ST^^FOWL^CA^999990000^^M|1|8885551212|8885551212|1|2||40007716^^^AccMgr^VN^1|12
3121234|||||||||||NO \r”

+"NK1|1|DUCK^HUEY|SO|3583 DUCK
RD^^FOWL^CA^999990000|8885552222||Y||||||||||||||\r"
+"PV1|1|I|PREOP^101^1^1^^^S|3|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|||01||||1|||37^DIS
NEY^WALT^^^^^^AccMgr^^^^CI|2|40007716^^^AccMgr^VN|4|||||||||||||||||||1||G|||20050
110045253|||||| GT1|1|8291|DUCK^DONALD^D||111^DUCK
ST^^FOWL^CA^999990000|8885551212||19241010|M||1|123121234||||#Cartoon Ducks
Inc|111^DUCK ST^^FOWL^CA^999990000|8885551212||PT|\r"

+"DG1|1|I9|71596^OSTEOARTHROS NOS-L/LEG ^I9|OSTEOARTHROS NOS-L/LEG ||A|


IN1|1|MEDICARE|3|MEDICARE|||||||Cartoon Ducks
Inc|19891001|||4|DUCK^DONALD^D|1|19241010|111^DUCK
ST^^FOWL^CA^999990000|||||||||||||||||123121234A||||||PT|M|111 DUCK
ST^^FOWL^CA^999990000|||||8291 IN2|1||123121234|Cartoon Ducks
Inc|||123121234A|||||||||||||||||||||||||||||||||||||||||||||||||||||||||8885551212\r"

+"IN1|2|NON-PRIMARY|9|MEDICAL MUTUAL CALIF.|PO BOX


94776^^HOLLYWOOD^CA^441414776||8003621279|PUBSUMB|||Cartoon Ducks
Inc||||7|DUCK^DONALD^D|1|19241010|111 DUCK
ST^^FOWL^CA^999990000|||||||||||||||||056269770||||||PT|M|111^DUCK
ST^^FOWL^CA^999990000|||||8291\r"

+"IN2|2||123121234|Cartoon Ducks
Inc||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||8885551212\r"

+"IN1|3|SELF PAY|1|SELF PAY|||||||||||5||1\r";

Output:-Should extract the fields between the pipilines

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashMap;

import java.util.Map;
public class CompareEachLine {

public static void main(String[] args) throws FileNotFoundException {

Map<String, String> element = new HashMap<String, String>();

String msg
="MSH|^~&|AccMgr|1|||20050110045504||ADT^A08|599102|P|2.3|abc|bui|mko|002|8duy|As
Cii|hl7|\r"

+"EVN|A01|20050110045502||||\r"

+"PID|1||10006579^^^1^MRN^1||DUCK^DONALD^D||19241010|M||1|111 DUCK
ST^^FOWL^CA^999990000^^M|1|8885551212|8885551212|1|2||40007716^^^AccMgr^VN^1|12
3121234|||||||||||NO \r"

+"NK1|1|DUCK^HUEY|SO|3583 DUCK
RD^^FOWL^CA^999990000|8885552222||Y||||||||||||||\r"

+"PV1|1|I|PREOP^101^1^1^^^S|3|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|||01||||1|||37^DIS
NEY^WALT^^^^^^AccMgr^^^^CI|2|40007716^^^AccMgr^VN|4|||||||||||||||||||1||G|||20050
110045253|||||| GT1|1|8291|DUCK^DONALD^D||111^DUCK
ST^^FOWL^CA^999990000|8885551212||19241010|M||1|123121234||||#Cartoon Ducks
Inc|111^DUCK ST^^FOWL^CA^999990000|8885551212||PT|\r"

+"DG1|1|I9|71596^OSTEOARTHROS NOS-L/LEG ^I9|OSTEOARTHROS


NOS-L/LEG ||A| IN1|1|MEDICARE|3|MEDICARE|||||||Cartoon Ducks
Inc|19891001|||4|DUCK^DONALD^D|1|19241010|111^DUCK
ST^^FOWL^CA^999990000|||||||||||||||||123121234A||||||PT|M|111 DUCK
ST^^FOWL^CA^999990000|||||8291 IN2|1||123121234|Cartoon Ducks
Inc|||123121234A|||||||||||||||||||||||||||||||||||||||||||||||||||||||||8885551212\r"

+"IN1|2|NON-PRIMARY|9|MEDICAL MUTUAL CALIF.|PO BOX


94776^^HOLLYWOOD^CA^441414776||8003621279|PUBSUMB|||Cartoon Ducks
Inc||||7|DUCK^DONALD^D|1|19241010|111 DUCK
ST^^FOWL^CA^999990000|||||||||||||||||056269770||||||PT|M|111^DUCK
ST^^FOWL^CA^999990000|||||8291\r"

+"IN2|2||123121234|Cartoon Ducks
Inc||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||8885551212\r"

+"IN1|3|SELF PAY|1|SELF PAY|||||||||||5||1\r";


String delimiter = "\\r";

String[] temp;

temp = msg.split(delimiter);

for (int i = 0; i < temp.length; i++) {

element.put("Segment-" + i, temp[i]);

for (Map.Entry me : element.entrySet()) {

System.out.println("Key: "+me.getKey() + "


& Value: " + me.getValue());

System.out.println("\n\n");

for (String value : element.values()){

String templ = value;

System.out.println(""+templ);

String check;

check = templ.substring(0, 3);

if (check.equalsIgnoreCase("MSH")){

System.out.println("it is the msh segment");

parsemsh(templ);

if (check.equalsIgnoreCase("PID")){

System.out.println("it is the pid segment");

parsepid(templ);

}
if (check.equalsIgnoreCase("EVN")){

System.out.println("it is the evn segment");

parseevn(templ);

if (check.equalsIgnoreCase("NK1"))

System.out.println("it is the nk1 segment");

parsenk1(templ);

if (check.equalsIgnoreCase("PV1"))

System.out.println("it is the pv1 segment");

parsepv1(templ);

if (check.equalsIgnoreCase("DG1"))

System.out.println("it is the DG1 segment");

parsedgi(templ);

if (check.equalsIgnoreCase("IN1"))

System.out.println("it is the in1 segment");

parsein1(templ);

if (check.equalsIgnoreCase("IN2"))
{

System.out.println("it is the In2 segment");

parsein2(templ);

private static void parsein2(String templ) {

Map<String, String> elementin2 = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementin2.put("IN2-" + i, temp[i]);

for (Map.Entry me : elementin2.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

private static void parsein1(String templ) {

Map<String, String> elementin1 = new HashMap<String, String>();


String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementin1.put("IN1-" + i, temp[i]);

for (Map.Entry me : elementin1.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

private static void parsepv1(String templ) {

Map<String, String> elementpv1 = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementpv1.put("PV1-" + i, temp[i]);

for (Map.Entry me : elementpv1.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());


}

private static void parsenk1(String templ) {

Map<String, String> elementpk1 = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementpk1.put("NK1-" + i, temp[i]);

for (Map.Entry me : elementpk1.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

private static void parseevn(String templ) {

Map<String, String> elementevn = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementevn.put("EVN-" + i, temp[i]);
}

System.out.print(temp);

for (Map.Entry me : elementevn.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

private static void parsepid(String templ) {

Map<String, String> elementpid = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementpid.put("PID-" + i, temp[i]);

for (Map.Entry me : elementpid.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

}
private static void parsedgi(String templ) {

Map<String, String> elementdgi = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementdgi.put("DGI-" + i, temp[i]);

for (Map.Entry me : elementdgi.entrySet()) {

System.out.println("");

System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());

private static void parsemsh(String templ) throws FileNotFoundException {

Map<String, String> elementmsh = new HashMap<String, String>();

String delimiters = "\\|";

String[] temp;

temp = templ.split(delimiters);

for (int i = 0; i < temp.length; i++) {

elementmsh.put("MSH-" + i, temp[i]);

elementmsh.put("messageheaderrsegment",
elementmsh.remove("MSH-01"));

}
String Messageheadersegment=temp[0];

String Encodingcharacter=temp[1];

String SendingApplication=temp[2];

String SendingFacility=temp[3];

String ReceivingApplication=temp[4];

String ReceivingFacility=temp[5];

String YYMMDDHHMMSS=temp[6];

String Security=temp[7];

String MessageType=temp[8];

String MessageControlId=temp[9];

String ProcessingId=temp[10];

String VersionId=temp[11];

String Sequencenumber=temp[12];

String ContinuationPointer=temp[13];

String AcceptAcknowledgmentType=temp[14];

String ApplicationAcknowledgmentType=temp[15];

String CountryCode=temp[16];

String CharacterSet=temp[17];

String PrincipalLanguageOfMessage=temp[18];

System.out.print("Messageheadersegment:"+Messageheadersegment);

System.out.print("\n");

System.out.print("Encodingcharacter: "+Encodingcharacter);

System.out.print("\n");

System.out.print("SendingApplication: "+SendingApplication);

System.out.print("\n");
System.out.print("SendingFacility: "+SendingFacility);

System.out.print("\n");

System.out.print("ReceivingApplication:
"+ReceivingApplication);

System.out.print("\n");

System.out.print("ReceivingFacility: "+ReceivingFacility);

System.out.print("\n");

System.out.print("YYMMDDHHMMSS: "+ YYMMDDHHMMSS);

System.out.print("\n");

System.out.print("Security: "+Security);

System.out.print("\n");

System.out.print("MessageType: "+MessageType);

System.out.print("\n");

System.out.print("MessageControlId: "+MessageControlId);

System.out.print("\n");

System.out.print("Version ID: "+VersionId);

System.out.print("\n");

System.out.print("Sequencenumber: "+Sequencenumber);

System.out.print("\n");

System.out.print("ContinuationPointer: "+ContinuationPointer);

System.out.print("\n");

System.out.print("AcceptAcknowledgmentType:
"+AcceptAcknowledgmentType);

System.out.print("\n");

System.out.print("CountryCode: "+CountryCode);

System.out.print("\n");

System.out.print("CharacterSet: "+CharacterSet);

System.out.print("\n");
System.out.print("PrincipalLanguageOfMessage:
"+PrincipalLanguageOfMessage);

System.out.print("\n");