-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
65 lines (57 loc) · 2.21 KB
/
NotesApp.java
File metadata and controls
65 lines (57 loc) · 2.21 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
import java.io.*;
import java.util.Scanner;
public class NotesApp {
private static final String FILE_NAME = "notes.txt";
// Method to write notes (append mode)
public static void writeNote(String note) {
try (FileWriter writer = new FileWriter(FILE_NAME, true)) { // true = append mode
writer.write(note + System.lineSeparator());
System.out.println("✅ Note saved successfully!");
} catch (IOException e) {
System.out.println("❌ Error writing to file: " + e.getMessage());
}
}
// Method to read all notes
public static void readNotes() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
System.out.println("\n📒 Your Notes:");
while ((line = reader.readLine()) != null) {
System.out.println("- " + line);
}
} catch (FileNotFoundException e) {
System.out.println("⚠ No notes found yet. Please add a note first.");
} catch (IOException e) {
System.out.println("❌ Error reading from file: " + e.getMessage());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n==== Notes App ====");
System.out.println("1. Add Note");
System.out.println("2. View Notes");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.print("Enter your note: ");
String note = scanner.nextLine();
writeNote(note);
break;
case 2:
readNotes();
break;
case 3:
System.out.println("👋 Exiting Notes App. Goodbye!");
break;
default:
System.out.println("❌ Invalid choice. Try again.");
}
} while (choice != 3);
scanner.close();
}
}