-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineUnifyer.java
More file actions
52 lines (45 loc) · 1.52 KB
/
Copy pathLineUnifyer.java
File metadata and controls
52 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.*;
import java.util.*;
/*
Reads from a file provided as input.
Merges lines that have been split across multiple lines with double-quotes.
Should add options to allows to merge by any delimiter.
http://ranthoranfell.blogspot.com/
*/
public class LineUnifyer{
public static void main(String[] args) throws IOException{
try{
File f = new File(args[0]);
Scanner s = new Scanner(f);
}catch(IOException io){
System.out.println("Cannot read from file.");
System.exit(-1);
}catch(ArrayOutOfBoundsException ae){
System.out.println("Usage: java LineUnifyer filename");
System.exit(-1);
}
ArrayList<String> lines = new ArrayList<String>();
while(s.hasNextLine()){
String thisLine = s.nextLine();
boolean quotedLineEnd = false;
//if the line contains both end quotes skip any more processing
if(thisLine.matches("^\".*$") && !thisLine.matches("^\".*\"?\"$")){
//if the line contains no end-quote we need to loop till we find one
while(!quotedLineEnd && s.hasNextLine()){
String nextLine = s.nextLine();
//if the line ends with special characters that continue into the next line
if(thisLine.matches(".*[\\p{Punct}&&[^\\s]]$"))
thisLine += nextLine;
else
thisLine += " " + nextLine; //adds a space to delimit the lines
if(nextLine.matches(".*\"$")){ //we've found our end character! stop!
quotedLineEnd = true;
}
}
}
lines.add(thisLine);
}
for(int i = 0 ; i < lines.size(); i++)
System.out.println(lines.get(i));
}
}