1
+ import java .io .*;
2
+ import java .util .ArrayList ;
3
+ import java .util .Scanner ;
4
+
5
+ public class TextRead {
6
+ public static void main (String [] args ) throws IOException {
7
+ // File to be read
8
+ File file = new File ("file.txt" );
9
+
10
+ // Use Scanner to read the file
11
+ Scanner scanner = new Scanner (file );
12
+
13
+ // Create a list to store the words
14
+ ArrayList <String > words = new ArrayList <>();
15
+
16
+ // Use a while loop to read through the file
17
+ while (scanner .hasNextLine ()) {
18
+ String line = scanner .nextLine ();
19
+
20
+ // Split the line by spaces
21
+ String [] lineWords = line .split (" " );
22
+
23
+ // Add each word to the list
24
+ for (String word : lineWords ) {
25
+ words .add (word );
26
+ }
27
+ }
28
+
29
+ // Close the scanner
30
+ scanner .close ();
31
+
32
+ // Display the words
33
+ System .out .println ("Original order: " );
34
+ for (String word : words ) {
35
+ System .out .println (word );
36
+ }
37
+
38
+ // Display the words in reverse order
39
+ System .out .println ("\n Reverse order: " );
40
+ for (int i = words .size () - 1 ; i >= 0 ; i --) {
41
+ System .out .println (words .get (i ));
42
+ }
43
+
44
+ // Display the words with all plurals capitalized
45
+ System .out .println ("\n Plurals capitalized: " );
46
+ for (int i = 0 ; i < words .size (); i ++) {
47
+ String word = words .get (i );
48
+ if (word .endsWith ("s" )) {
49
+ word = word .toUpperCase ();
50
+ words .set (i , word );
51
+ }
52
+ System .out .println (word );
53
+ }
54
+
55
+ // Display the words with all plural words removed
56
+ System .out .println ("\n Plurals removed: " );
57
+ for (int i = 0 ; i < words .size (); i ++) {
58
+ String word = words .get (i );
59
+ if (word .endsWith ("s" )) {
60
+ words .remove (i );
61
+ i --;
62
+ }
63
+ }
64
+ for (String word : words ) {
65
+ System .out .println (word );
66
+ }
67
+ }
68
+ }
0 commit comments