rob345
rob345

Reputation: 203

String concatenation in java

I have output from an old terminal system that needs to be concatenated. Here is an example of the output.

output1 = 
LINE ONE
LINE TWO 
LINE THREE
NO DATA
LINE FOUR
LINE FIVE
LINE SIX         %

output2 = 
LINE FOUR        %
LINE FIVE 
LINE SIX 
LINE SEVEN
NO DATA
LINE EIGHT
END

there may be as many as 5 output strings that need to be joined. Problem ... There may be duplicates among the lines (there are some repeats (ex: NO DATA) that should not be removed), so a simple line comparison is not workable.

correct answer should be:
LINE ONE
LINE TWO 
LINE THREE
NO DATA
LINE FOUR
LINE FIVE
LINE SIX 
LINE SEVEN
NO DATA
LINE EIGHT
END

Looking for a java solution.

Any ideas? Thanks.

Upvotes: 1

Views: 306

Answers (2)

Jason Buberel
Jason Buberel

Reputation: 5010

If you receive the data as one large file, you should start by taking a look at the Scanner object, as in:

File f = new File("thefile.txt");
Scanner s = new Scanner(new BufferedReader(new FileReader(f))).useDelimiter("END");
while (s.hasNext()) {
    String block = s.next(); // string will now contain all text between instances of "END"
    // process the text block by splitting on \n
    String[] lines = block.split("\n");
    StringBuilder output = new StringBuilder();
    for ( String line : lines ) {
       // process each line, checking for duplicates, appending to output.
    }
    // write your final output to a file, etc.
}

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240966

Hints:

read each line of output

create an List

check

if (!"NO DATA".equals(thisLine) {
    if(!list.contains(thisLine)){ /* add to list */}
}else{
    /* add to list */
}

Upvotes: 1

Related Questions