-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
72 lines (62 loc) · 2.22 KB
/
NotesApp.java
File metadata and controls
72 lines (62 loc) · 2.22 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
import java.io.*;
import java.util.Scanner;
public class NotesApp {
private static final String FILE_NAME = "notes.txt";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n--- Simple Notes App ---");
System.out.println("1. Write a new note");
System.out.println("2. View all notes");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
writeNote(scanner);
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();
}
private static void writeNote(Scanner scanner) {
try {
FileWriter writer = new FileWriter(FILE_NAME, true); // append mode
System.out.print("Enter your note: ");
String note = scanner.nextLine();
writer.write(note + "\n");
writer.close();
System.out.println("Note saved successfully.");
} catch (IOException e) {
System.out.println("Error writing note: " + e.getMessage());
}
}
private static void readNotes() {
try {
File file = new File(FILE_NAME);
if (!file.exists()) {
System.out.println("No notes found.");
return;
}
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
System.out.println("\n--- Your Notes ---");
while ((line = reader.readLine()) != null) {
System.out.println("- " + line);
}
reader.close();
} catch (IOException e) {
System.out.println("Error reading notes: " + e.getMessage());
}
}
}