-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSort.java
More file actions
55 lines (47 loc) · 1.42 KB
/
WordSort.java
File metadata and controls
55 lines (47 loc) · 1.42 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
53
54
55
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Collections;
import java.util.Iterator;
public class WordSort {
public static void main( String[] args ) throws Exception {
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
Scanner src = new Scanner(stdIn);
CopyOnWriteArrayList<String> wordList = new CopyOnWriteArrayList<String>();
int fromIndex = 0;
int toIndex = 0;
int count = 0;
String s = "";
src.useDelimiter("[.,;:\\n\\s-]+"); //sets delimeters
if ( args.length == 1 && "-sensitive".equals( args[0] ) ) { //case sensitive method
while ( src.hasNext() ) {
wordList.add( src.next() );
}
}
else if (args.length == 0) { //upper case method
while ( src.hasNext() ) {
wordList.add( src.next().toUpperCase() );
}
}
else {
throw new IllegalArgumentException(); //exception if args incorrect
}
Collections.sort( wordList );
Iterator<String> it = wordList.iterator();
while( it.hasNext() ) {
s = it.next();
fromIndex = wordList.indexOf( s );
toIndex = wordList.lastIndexOf( s );
count = toIndex - fromIndex + 1;
if ( wordList.contains( s ) ) {
wordList.set( fromIndex, s + " " + count );
}
if ( it.hasNext() ) {
wordList.subList( fromIndex + 1, toIndex + 1 ).clear();
}
}
System.out.println(wordList);
}
}