Spaces:
Running
Running
catch (FileNotFoundException e) {...}
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/12B. Names to a File
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a program that asks the user for names.
|
| 2 |
+
The given names are added to a file named 'names.txt' such that each name is on its own line.
|
| 3 |
+
When the user enters the name "stop", the program execution ends.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
import java.util.Random;
|
| 9 |
+
import java.util.ArrayList;
|
| 10 |
+
import java.util.Arrays;
|
| 11 |
+
import java.io.FileNotFoundException;
|
| 12 |
+
import java.util.Collections;
|
| 13 |
+
import java.util.Scanner;
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
public class Test{
|
| 18 |
+
public static void main(String[] args){
|
| 19 |
+
final Random r = new Random();
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
//ADD
|
| 23 |
+
Scanner reader = new Scanner(System.in);
|
| 24 |
+
ArrayList<String> names = new ArrayList<String>();
|
| 25 |
+
|
| 26 |
+
// get user input 'name'
|
| 27 |
+
while (true) {
|
| 28 |
+
System.out.print("Enter a name: ");
|
| 29 |
+
String name = String.valueOf(reader.nextLine());
|
| 30 |
+
|
| 31 |
+
// break if user inputs "stop"
|
| 32 |
+
if (name.equals("stop")) {
|
| 33 |
+
break;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// add name to ArrayList
|
| 37 |
+
names.add(name);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
// add name to the file, separated by newline "\n"
|
| 42 |
+
try (PrintWriter file = new PrintWriter("names.txt")) {
|
| 43 |
+
names.forEach(name -> file.write(name + "\n"));
|
| 44 |
+
}
|
| 45 |
+
catch (FileNotFoundException e) {
|
| 46 |
+
System.out.println("An error occurred: " + e);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
System.out.println("File content:");
|
| 51 |
+
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
Enter a name: Jack
|
| 57 |
+
Enter a name: Sophie
|
| 58 |
+
Enter a name: Oliver
|
| 59 |
+
Enter a name: Harry
|
| 60 |
+
Enter a name: Harry
|
| 61 |
+
Enter a name: Jack
|
| 62 |
+
Enter a name: Sophie
|
| 63 |
+
Enter a name: Lily
|
| 64 |
+
Enter a name: Oliver
|
| 65 |
+
Enter a name: Emily
|
| 66 |
+
Enter a name: Emily
|
| 67 |
+
Enter a name: Lily
|
| 68 |
+
Enter a name: stop
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
File content:
|
| 72 |
+
Jack
|
| 73 |
+
Sophie
|
| 74 |
+
Oliver
|
| 75 |
+
Harry
|
| 76 |
+
Harry
|
| 77 |
+
Jack
|
| 78 |
+
Sophie
|
| 79 |
+
Lily
|
| 80 |
+
Oliver
|
| 81 |
+
Emily
|
| 82 |
+
Emily
|
| 83 |
+
Lily
|
| 84 |
+
|
| 85 |
+
|