Created
May 1, 2016 18:38
-
-
Save Jaskaranbir/839bec6b7f5cdc3dd6a1e694540d718c to your computer and use it in GitHub Desktop.
A manual way to convert a given set of parallel strings in Tabular Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String[] generateTable(ArrayList<String> lines){ | |
// A list to store every single element individually | |
ArrayList<String> sl = new ArrayList<>(); | |
int length = lines.get(0).split(" ").length; // Number of individual | |
// elements | |
// Create an array of every element individually | |
for (String line : lines) { | |
String[] sp = line.split(" "); | |
for (String semiChars : sp) | |
sl.add(semiChars); | |
} | |
// This controls iteration | |
int j = 1; | |
for (int i = 0; i < sl.size() / lines.size(); i++) { | |
int maxLength = 0; // Max length of character within that set | |
String fixStr = ""; // String with fixed spaces | |
// Determine maximum length from set | |
// Compares the element lengths in multiples. Something like: | |
// (element i, element i+length, element i+length+length.....) | |
while (j < sl.size()) { | |
fixStr = sl.get(i + j - 1); | |
if (maxLength < sl.get(i + j - 1).length()) | |
maxLength = sl.get(i + j - 1).length(); | |
j += length; | |
} | |
j = 1; // Ready for another loop | |
// Add spaces to match max length in set | |
while (j < sl.size()) { | |
fixStr = ""; | |
// Gets elements in multiples (same as above loop) | |
if (sl.get(i + j - 1).length() < maxLength) { | |
fixStr = sl.get(i + j - 1); | |
while (fixStr.length() < maxLength) | |
fixStr += " "; | |
sl.set(i + j - 1, fixStr); | |
} | |
j += length; | |
} | |
j = 1; // Ready for another loop | |
} | |
// Create final array that is returned | |
String semiElement = ""; // Stores elements as sets of lines | |
String[] sortedArray = new String[lines.size()]; // The final Array | |
// Form expression and add it to final Array | |
for (int i = 0; i < lines.size(); i++) { | |
j = 0; | |
while (j < length) { | |
// Add as many spaces as you want between elements here | |
semiElement += (sl.get((i * length) + j) + " "); | |
j++; | |
} | |
sortedArray[i] = semiElement; | |
semiElement = ""; | |
} | |
return sortedArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment