- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
Latest commit
72 lines (62 loc) · 2.22 KB
/
Copy pathNotesApp.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
importjava.io.*;
importjava.util.Scanner;
publicclassNotesApp {
privatestaticfinalStringFILE_NAME = "notes.txt";
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
intchoice;
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) {
case1:
writeNote(scanner);
break;
case2:
readNotes();
break;
case3:
System.out.println("Exiting Notes App. Goodbye!");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (choice != 3);
scanner.close();
}
privatestaticvoidwriteNote(Scannerscanner) {
try {
FileWriterwriter = newFileWriter(FILE_NAME, true); // append mode
System.out.print("Enter your note: ");
Stringnote = scanner.nextLine();
writer.write(note + "\n");
writer.close();
System.out.println("Note saved successfully.");
} catch (IOExceptione) {
System.out.println("Error writing note: " + e.getMessage());
}
}
privatestaticvoidreadNotes() {
try {
Filefile = newFile(FILE_NAME);
if (!file.exists()) {
System.out.println("No notes found.");
return;
}
BufferedReaderreader = newBufferedReader(newFileReader(file));
Stringline;
System.out.println("\n--- Your Notes ---");
while ((line = reader.readLine()) != null) {
System.out.println("- " + line);
}
reader.close();
} catch (IOExceptione) {
System.out.println("Error reading notes: " + e.getMessage());
}
}
}