Skip to content

Commit 490acd8

Browse files
committed
✨ Create TextRead.java ✨
1 parent 3411d7c commit 490acd8

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

TextRead.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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("\nReverse 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("\nPlurals 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("\nPlurals 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+
}

file.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all
2+
texts
3+
are
4+
ordered
5+
and
6+
compiled

0 commit comments

Comments
 (0)