-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
55 lines (54 loc) · 1.53 KB
/
Copy pathNotesApp.java
File metadata and controls
55 lines (54 loc) · 1.53 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
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);
System.out.println("Welcome to Notes App!");
while (true) {
System.out.println("\nChoose an option:");
System.out.println("1. Write a note");
System.out.println("2. Read notes");
System.out.println("3. Exit");
System.out.print("Your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
writeNote(scanner);
break;
case 2:
readNotes();
break;
case 3:
System.out.println("Goodbye!");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
private static void writeNote(Scanner scanner) {
System.out.print("Enter your note: ");
String note = scanner.nextLine();
try (FileWriter writer = new FileWriter(FILE_NAME, true)) {
writer.write(note + System.lineSeparator());
System.out.println("Note saved.");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
private static void readNotes() {
System.out.println("\nYour Notes:");
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("- " + line);
}
} catch (FileNotFoundException e) {
System.out.println("No notes found. Start by writing one!");
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}