Skip to content

Instantly share code, notes, and snippets.

@rishpandey
Created August 3, 2017 11:51
Show Gist options
  • Save rishpandey/859210f7c351df83b927917be583572e to your computer and use it in GitHub Desktop.
Save rishpandey/859210f7c351df83b927917be583572e to your computer and use it in GitHub Desktop.
package problems;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class FileAppender {
public static void main(String[] args) {
InputStream in = System.in;
Scanner scanner = new Scanner(in);
int totalFiles = Integer.parseInt(scanner.nextLine());
String str = "";
ArrayList<String> array = new ArrayList<String>();
String temp = "";
int counter = 0;
// press CTRL+D to stop STDIN after data input
// Store input from STDIN
while (scanner.hasNextLine()) {
temp = scanner.nextLine();
counter++;
if (temp.length() > 0) {
counter = 0;
array.add(temp);
} else {
if (counter == 3) {
array.add("-");
}
}
}
String[] list = new String[array.size()];
String[] result = new String[array.size()];
list = array.toArray(new String[array.size()]);
// Replace $ and append file names at first index
for (int i = 0, j = 0; i < list.length; i++) {
result[j++] = list[i].replaceAll("\\$", "");
if (list[i].equals("-")) {
list[0] = list[0].split("\\.")[0] + list[i + 1].substring(0, 1).toUpperCase()
+ list[i + 1].substring(1);
i = i + 1;
}
}
result[0] = list[0];
// Write to STDOUT
for (String s : result) {
if (s != null && !s.equals("-"))
System.out.println(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment