-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringFormatter.java
More file actions
45 lines (41 loc) · 1.29 KB
/
StringFormatter.java
File metadata and controls
45 lines (41 loc) · 1.29 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
import java.util.ArrayList;
import java.util.List;
public class StringFormatter
{
public static int totalLetters( List<String> wordList )
{
int sum = 0;
for(int i=0; i<wordList.size(); i++) {
sum+=wordList.get(i).length();
}
return sum;
}
public static int basicGapWidth( List<String> wordList, int formattedLen )
{
int gap = ((formattedLen-totalLetters(wordList))/(wordList.size()-1));
return gap;
}
public static int leftoverSpaces( List<String> wordList, int formattedLen)
{
int left = (formattedLen-totalLetters(wordList)-((wordList.size()-1)*basicGapWidth(wordList,formattedLen)));
return left;
}
public static String format( List<String> wordList, int formattedLen )
{
String form = "";
form+=wordList.get(0);
String gap="";
for(int i=0; i<basicGapWidth(wordList,formattedLen); i++) {
gap+= " ";
}
for(int i=1; i<wordList.size(); i++) {
if(leftoverSpaces(wordList,formattedLen)>0) {
form+= gap+" "+wordList.get(i);
}
else {
form+= gap + wordList.get(i);
}
}
return form;
}
}