I guess that you could get the index of the header (A, B, ...) in the String and compare it to the index of the value in each lines to get the closest ... I tried quickly and got this result :
public static void main(String[] args) {
String headerColumn = " A B C D E";
String firstLine = " 1 2 3";
String secondLine = " 4 6 7 ";
Map<Integer, String> indexHeaderMap = new HashMap<Integer, String>();
// Get header indexes
for (int i = 0; i < headerColumn.length(); i++) {
String currChar = String.valueOf(headerColumn.charAt(i));
if (!currChar.equals(" ")) {
indexHeaderMap.put(i, currChar);
}
}
// Parse first line
parseLine(firstLine, indexHeaderMap);
// Parse second line
parseLine(secondLine, indexHeaderMap);
}
And the functions :
private static void parseLine(String pLine, Map<Integer, String> pHeaderMap) {
for (int i = 0; i < pLine.length(); i++) {
String currChar = String.valueOf(pLine.charAt(i));
if (!currChar.equals(" ")) {
int valueColumnIndex = getNearestColumnIndex(i, pHeaderMap);
System.out.println("Value " + currChar + " is on column " + pHeaderMap.get(valueColumnIndex));
}
}
}
private static int getNearestColumnIndex(int pIndex,
Map<Integer, String> pHeaderMap) {
int minDiff = 500;
int nearestColumnIndex = -1;
for(Map.Entry<Integer, String> mapEntry : pHeaderMap.entrySet()) {
int diff = Math.abs(mapEntry.getKey() - pIndex);
if (diff < minDiff) {
minDiff = diff;
nearestColumnIndex = mapEntry.getKey();
}
}
return nearestColumnIndex;
}
Here's the output :
Value 1 is on column A
Value 2 is on column B
Value 3 is on column E
Value 4 is on column A
Value 6 is on column C
Value 7 is on column D
I hope this is helpful enough to get the result you expect !