-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatSentences.java
More file actions
379 lines (343 loc) · 10.2 KB
/
FormatSentences.java
File metadata and controls
379 lines (343 loc) · 10.2 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*Sudipti Dantuluri
* 9.16.2021
* FormatSentences.java
* The program uses scanner to print three sentences in either printf or format*/
import java.util.Scanner; //Import library
public class FormatSentences
{
public static void main (String [] args)
{
FormatSentences msp = new FormatSentences();
msp.types();
}
public void types()
{
Scanner types = new Scanner(System.in); //Opens the Scanner
int orientation = 0; // left(1), center(2), or right(3)? ");
System.out.print("\n\n\n");
System.out.print("Printf(1) or Format(2)? ");
int print_fmt = types.nextInt();
System.out.print("Would you like left(1), center(2), or right(3)? ");
int sentence1_justify = types.nextInt();
System.out.print("How many spaces? ");
int sentence1_spaces = types.nextInt();
types.nextLine();
System.out.print("Enter your sentence: ");
String sentence1 = types.nextLine();
types.nextLine();
System.out.print("Would you like left(1), center(2), or right(3)? ");
int sentence2_justify = types.nextInt();
System.out.print("How many spaces? ");
int sentence2_spaces = types.nextInt();
types.nextLine();
System.out.print("Enter your sentence: ");
String sentence2 = types.nextLine();
types.nextLine();
System.out.print("Would you like left(1), center(2), or right(3)? ");
int sentence3_justify = types.nextInt();
System.out.print("How many spaces? ");
int sentence3_spaces = types.nextInt();
types.nextLine();
System.out.print("Enter your sentence: ");
String sentence3 = types.nextLine();
types.nextLine();
if (print_fmt==1) //Printf
{
//use printf
String format;
System.out.println("0123456789012345678901234567890123456789");
if (sentence1_justify == 1) {
format = "%-" + sentence1_spaces + "s\n";
System.out.printf(format, sentence1);
}
if (sentence1_justify == 2) {
int spaces = (sentence1_spaces - sentence1.length())/2;
format = "%" + spaces + "s" + "%" + sentence1.length() +"s" + "%" + spaces + "s\n";
System.out.printf(format, " ", sentence1, " ");
}
if (sentence1_justify == 3) {
format = "%" + sentence1_spaces + "s\n";
System.out.printf(format, sentence1);
}
System.out.println("0123456789012345678901234567890123456789");
if (sentence2_justify == 1) {
format = "%-" + sentence2_spaces + "s\n";
System.out.printf(format, sentence2);
}
if (sentence2_justify == 2) {
int spaces = (sentence2_spaces - sentence2.length())/2;
format = "%" + spaces + "s" + "%" + sentence2.length() +"s" + "%" + spaces + "s\n";
System.out.printf(format, " ", sentence2, " ");
}
if (sentence2_justify == 3) {
format = "%" + sentence2_spaces + "s\n";
System.out.printf(format, sentence2);
}
System.out.println("0123456789012345678901234567890123456789");
if (sentence3_justify == 1) {
format = "%-" + sentence3_spaces + "s\n";
System.out.printf(format, sentence3);
}
if (sentence3_justify == 2) {
int spaces = (sentence3_spaces - sentence3.length())/2;
format = "%" + spaces + "s" + "%" + sentence3.length() +"s" + "%" + spaces + "s\n";
System.out.printf(format, " ", sentence3, " ");
}
if (sentence3_justify == 3) {
format = "%" + sentence3_spaces + "s\n";
System.out.printf(format, sentence3);
}
}
if (print_fmt==2) //Format
{
System.out.println("Here are your three sentences: ");
System.out.println("0123456789012345678901234567890123456789");
if (sentence1_justify == 1) {
System.out.println(Format.left(sentence1,sentence1_spaces));
}
if (sentence1_justify == 2) {
System.out.println(Format.center(sentence1,sentence1_spaces));
}
if (sentence1_justify == 3) {
System.out.println(Format.right(sentence1,sentence1_spaces));
}
System.out.println("0123456789012345678901234567890123456789");
if (sentence2_justify == 1) {
System.out.println(Format.left(sentence2,sentence2_spaces));
}
if (sentence2_justify == 2) {
System.out.println(Format.center(sentence2,sentence2_spaces));
}
if (sentence2_justify == 3) {
System.out.println(Format.right(sentence2,sentence3_spaces));
}
System.out.println("0123456789012345678901234567890123456789");
if (sentence3_justify == 1) {
System.out.println(Format.left(sentence3,sentence2_spaces));
}
if (sentence3_justify == 2) {
System.out.println(Format.center(sentence3,sentence3_spaces));
}
if (sentence3_justify == 3) {
System.out.println(Format.right(sentence3,sentence3_spaces));
}
}
}
}
class Format
{
public Format()
{
}
public static String center(double d, int i, int j)
{
String s = decimalPlaces(d, j);
if(i < s.length())
return s;
else
return pad(' ', ((i - s.length()) + 1) / 2) + s +
pad(' ', (i - s.length()) / 2);
}
public static String centerSigFigs(double d, int i, int j)
{
String s = sigFigFix(d, j);
if(i < s.length())
return s;
else
return pad(' ', ((i - s.length()) + 1) / 2) + s +
pad(' ', (i - s.length()) / 2);
}
public static String center(long l, int i)
{
String s = convert(l);
if(i < s.length())
return s;
else
return pad(' ', ((i - s.length()) + 1) / 2) + s +
pad(' ', (i - s.length()) / 2);
}
public static String center(String s, int i)
{
if(i < s.length())
return s;
else
return pad(' ', ((i - s.length()) + 1) / 2) + s +
pad(' ', (i - s.length()) / 2);
}
public static String left(double d, int i, int j)
{
String s = decimalPlaces(d, j);
if(i < s.length())
return s;
else
return s + pad(' ', i - s.length());
}
public static String leftSigFigs(double d, int i, int j)
{
String s = sigFigFix(d, j);
if(i < s.length())
return s;
else
return s + pad(' ', i - s.length());
}
public static String left(long l, int i)
{
String s = convert(l);
if(i < s.length())
return s;
else
return s + pad(' ', i - s.length());
}
public static String left(String s, int i)
{
if(i < s.length())
return s;
else
return s + pad(' ', i - s.length());
}
public static String right(double d, int i, int j)
{
String s = decimalPlaces(d, j);
if(i < s.length())
return s;
else
return pad(' ', i - s.length()) + s;
}
public static String rightSigFigs(double d, int i, int j)
{
String s = sigFigFix(d, j);
if(i < s.length())
return s;
else
return pad(' ', i - s.length()) + s;
}
public static String right(long l, int i)
{
String s = convert(l);
if(i <= s.length())
return s;
else
return pad(' ', i - s.length()) + s;
}
public static String right(String s, int i)
{
if(i < s.length())
return s;
else
return pad(' ', i - s.length()) + s;
}
public static String decimalPlaces(double d, int i)
{
double d1 = d * Math.pow(10D, i);
d1 = Math.round(d1);
d1 /= Math.pow(10D, i);
return convert(d1, i);
}
public static String sigFigFix(double d, int i)
{
String s = Double.toString(d);
String s1;
String s2;
if(s.startsWith("-") || s.startsWith("+"))
{
s1 = s.substring(0, 1);
s2 = s.substring(1);
} else
{
s1 = "";
s2 = s;
}
int j = s2.indexOf(101);
if(j == -1)
j = s2.indexOf(69);
String s3;
String s4;
if(j == -1)
{
s3 = s2;
s4 = "";
} else
{
s3 = s2.substring(0, j);
s4 = s2.substring(j);
}
int k = s3.indexOf(46);
StringBuffer stringbuffer;
StringBuffer stringbuffer1;
if(k == -1)
{
stringbuffer = new StringBuffer(s3);
stringbuffer1 = new StringBuffer("");
} else
{
stringbuffer = new StringBuffer(s3.substring(0, k));
stringbuffer1 = new StringBuffer(s3.substring(k + 1));
}
int l = stringbuffer.length();
int i1 = stringbuffer1.length();
if((l == 0 || stringbuffer.equals("0")) && i1 > 0)
{
l = 0;
for(int j1 = 0; j1 < stringbuffer1.length(); j1++)
{
if(stringbuffer1.charAt(j1) != '0')
break;
i1--;
}
}
int k1 = l + i1;
if(i > k1)
{
for(int l1 = k1; l1 < i; l1++)
stringbuffer1.append('0');
} else
if(i < k1 && i >= l)
stringbuffer1.setLength(
stringbuffer1.length() - (i1 - (i - l)));
else
if(i < l)
{
stringbuffer1.setLength(0);
for(int i2 = i; i2 < l; i2++)
stringbuffer.setCharAt(i2, '0');
}
if(stringbuffer1.length() == 0)
return s1 + stringbuffer + s4;
else
return s1 + stringbuffer + "." + stringbuffer1 + s4;
}
private static String convert(double d, int i)
{
int j = 0;
StringBuffer stringbuffer = new StringBuffer("" + d);
int k;
for(k = stringbuffer.length(); j < k &&
stringbuffer.charAt(j) != '.'; j++);
int l;
if(j == k)
{
stringbuffer.append(".");
l = i;
} else
{
int i1 = k - j - 1;
l = i - i1;
}
for(int j1 = 0; j1 < l; j1++)
stringbuffer.append("0");
return stringbuffer.toString();
}
private static String convert(long l)
{
return "" + l;
}
private static String pad(char c, int i)
{
StringBuffer stringbuffer = new StringBuffer("");
if(i < 1)
return "";
for(int j = 0; j < i; j++)
stringbuffer.append(c);
return stringbuffer.toString();
}
}