-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLongWordFile
More file actions
157 lines (121 loc) · 4.04 KB
/
LongWordFile
File metadata and controls
157 lines (121 loc) · 4.04 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
# LongWordFile Program
📄 **Description**:
This Java program reads a file provided by the user and identifies the **longest word** in the file. It incorporates robust error handling and ensures resources are properly closed after use.
---
## 🛠️ **How It Works**
1. The program prompts the user to enter the file path.
2. It validates whether the provided path points to a valid file.
3. Reads the file word by word.
4. Compares the length of each word to find the **longest word**.
5. Displays the longest word to the user.
---
## 📂 **Structure**
### **Class**:
- `LongWordFile`: Contains the main logic.
### **Methods**:
1. `main(String[] args)`:
- Entry point that calls the `findLongestWords()` method.
- Handles exceptions during file reading.
2. `findLongestWords()`:
- Reads the file, processes it, and finds the longest word.
- Handles user input and resource management.
### **Variables**:
- `iScan`: Scanner for user input.
- `filePath`: Stores the file path entered by the user.
- `file`: Represents the file object.
- `longestWord`: Tracks the longest word found in the file.
- `currentWord`: Temporarily holds the current word being read.
- `fScan`: Scanner for reading the file.
---
## 🚀 **How to Run**
1. Compile the program:
```bash
javac LongWordFile.java
```
2. Run the program:
```bash
java LongWordFile
```
3. Enter the file path when prompted:
```
Enter the file path: /path/to/your/file.txt
```
---
## 🛡️ **Error Handling**
- If the file is not found or the path is invalid, the program displays a user-friendly error message:
```
Error: File not found. Please ensure the file path is correct.
```
---
## 📋 **Example Input/Output**
### Input:
```
Enter the file path: /home/user/documents/test.txt
```
### File Contents:
```txt
Java is a wonderful programming language!
```
### Output:
```
The longest word in the file is: programming
```
---
## ✨ **Features**
- Dynamic file path input.
- Handles missing or invalid files gracefully.
- Closes all resources properly after use.
- Simple and user-friendly.
---
## 📂 **File Example**
Create a sample text file (e.g., `test.txt`) with some content to test the program:
```txt
This program is designed to find the longest word in this file.
```
---
## 🖥️ **System Requirements**
- Java Development Kit (JDK) 8 or higher.
- Any operating system with Java installed.
##Code
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class LongWordFile {
public static void main(String[] args) {
try {
new LongWordFile().findLongestWords();
} catch (FileNotFoundException e) {
System.out.println("Error: File not found. Please ensure the file path is correct.");
}
}
public void findLongestWords() throws FileNotFoundException {
Scanner iScan = new Scanner(System.in);
System.out.print("Enter the file path: ");
String filePath = iScan.nextLine();
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
System.out.println("The specified path is not a valid file. Please try again.");
iScan.close();
return;
}
String longestWord = "";
String currentWord;
Scanner fScan = new Scanner(file);
while (fScan.hasNext()) {
currentWord = fScan.next();
if (currentWord.length() > longestWord.length()) {
longestWord = currentWord;
}
}
System.out.println("\nThe longest word in the file is: " + longestWord);
fScan.close();
iScan.close();
}
}
---
This optimized version improves readability and ensures the logic is easy to follow.
🛠️ How to Use Clone this repo
git clone https://github.com/Warlord27/JavaProb.git Navigate and open any .java file
Compile & run with your favorite Java IDE or CLI
⭐ Contributions Welcome! Got a cool Java problem? Found a bug? Feel free to fork, improve, or suggest! 🙌
Made with ☕ and ❤️ by Adam Warlord