Skip to content

Commit 60f159c

Browse files
authored
Merge pull request #30 from KAUSHIK-KUMAR-RM/add-motivational-quote-script
Added Motivational Quote Generator
2 parents 02d7a60 + 19e6d41 commit 60f159c

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.*;
4+
5+
public class MotivationalQuote {
6+
7+
private static final String QUOTE_FILE = "quotes.txt";
8+
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in);
11+
System.out.println("Choose an option:");
12+
System.out.println("1. Show random local quote");
13+
System.out.println("2. Fetch quote from API");
14+
System.out.print("Enter choice: ");
15+
16+
int choice = scanner.nextInt();
17+
scanner.close();
18+
19+
switch (choice) {
20+
case 1:
21+
showRandomLocalQuote();
22+
break;
23+
case 2:
24+
fetchQuoteFromAPI();
25+
break;
26+
default:
27+
System.out.println("Invalid choice!");
28+
}
29+
}
30+
31+
// Option 1: Read from local file
32+
private static void showRandomLocalQuote() {
33+
List<String> quotes = new ArrayList<>();
34+
try (BufferedReader br = new BufferedReader(new FileReader(QUOTE_FILE))) {
35+
String line;
36+
while ((line = br.readLine()) != null) {
37+
if (!line.trim().isEmpty()) {
38+
quotes.add(line.trim());
39+
}
40+
}
41+
42+
if (quotes.isEmpty()) {
43+
System.out.println("No quotes found in file!");
44+
return;
45+
}
46+
47+
Random random = new Random();
48+
int index = random.nextInt(quotes.size());
49+
System.out.println("\n💬 Random Quote:\n" + quotes.get(index));
50+
51+
} catch (IOException e) {
52+
System.out.println("Error reading quotes file: " + e.getMessage());
53+
}
54+
}
55+
56+
// Option 2: Fetch from public API
57+
private static void fetchQuoteFromAPI() {
58+
try {
59+
URL url = new URL("https://api.quotable.io/random");
60+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
61+
conn.setRequestMethod("GET");
62+
63+
if (conn.getResponseCode() == 200) {
64+
BufferedReader in = new BufferedReader(
65+
new InputStreamReader(conn.getInputStream()));
66+
StringBuilder response = new StringBuilder();
67+
String line;
68+
69+
while ((line = in.readLine()) != null) {
70+
response.append(line);
71+
}
72+
in.close();
73+
74+
// Simple extraction of the "content" field
75+
String json = response.toString();
76+
String quote = json.split("\"content\":\"")[1].split("\",")[0];
77+
System.out.println("\n🌐 Fetched Quote:\n" + quote);
78+
} else {
79+
System.out.println("Failed to fetch quote from API.");
80+
}
81+
82+
} catch (Exception e) {
83+
System.out.println("Error fetching quote: " + e.getMessage());
84+
}
85+
}
86+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 💬 Motivational Quote Generator
2+
3+
### A simple Java program that displays a random motivational quote each time it runs.
4+
5+
---
6+
Features
7+
- Shows a random quote from a local file (quotes.txt)
8+
- Option to fetch a quote from the internet using the Quotable API (https://api.quotable.io)
9+
- Beginner-friendly Java example using:
10+
- Random
11+
- File I/O
12+
- HttpURLConnection
13+
---
14+
🏗️ How to Run
15+
1. Clone this repository or download the files.
16+
2. Make sure quotes.txt is in the same directory as MotivationalQuote.java.
17+
3. Compile and run:
18+
javac MotivationalQuote.java
19+
java MotivationalQuote
20+
4. Choose an option:
21+
- 1 → Show a random local quote
22+
- 2 → Fetch from API
23+
---
24+
Example Output
25+
26+
Choose an option:
27+
1. Show random local quote
28+
2. Fetch quote from API
29+
30+
Enter choice: 1
31+
32+
💬 Random Quote:
33+
Dream big. Start small. Act now.
34+
35+
---
36+
📚 Tech Used
37+
38+
• Java
39+
• File I/O
40+
• Random class
41+
• Basic API handling (HttpURLConnection)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Believe you can and you're halfway there.
2+
Success is not final, failure is not fatal: it is the courage to continue that counts.
3+
Hard work beats talent when talent doesn't work hard.
4+
Dream big. Start small. Act now.
5+
Don't watch the clock; do what it does. Keep going.

0 commit comments

Comments
 (0)