-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBabyNames.java
More file actions
94 lines (83 loc) · 2.1 KB
/
BabyNames.java
File metadata and controls
94 lines (83 loc) · 2.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Name: Rishi Saravanan
//Date: 1/29/22
//Program Name: BabyNames
//Program Goal: Use file, printWriter, and append to add more babyNames to file
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class BabyNames {
Scanner input = null;
Scanner s1 = new Scanner(System.in);
PrintWriter pw = null;
public static void main(String[] args) {
BabyNames b1 = new BabyNames();
try {
b1.runner();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void runner() throws Exception{
int counter = numOfRows();
openFile();
printValues();
closeFile();
// open the file again
openFile();
System.out.print("\nWould you like to add to the list, yes(1) and no(2)");
int choice = s1.nextInt();
if(choice == 1) {
System.out.println("Please enter the name and gender. If done, enter dunzo!");
String newName = s1.next();
String newGender = s1.next();
while(!newName.equals("Dunzo")) {
pw.println(counter++ + ") " + newName + " " + newGender );
s1.nextLine();
newName = s1.next();
if (newName.equals("Dunzo!"))
break;
newGender = s1.next();
}
closeFile();
openFile();
System.out.println("Here is your updated list: ");
printValues();
closeFile();
}
}
public void openFile() {
File inFileName = new File("BabyNames.txt1");
try {
pw = new PrintWriter(new FileWriter(inFileName, true));
input = new Scanner(inFileName);
} catch(IOException e) {
e.printStackTrace();
System.err.println("Cannot append to " + "output file");
System.exit(1);
}
}
public void closeFile() {
pw.close();
input.close();
}
public int numOfRows() {
openFile();
int start = 0;
while (input.hasNextLine()) {
start++;
input.nextLine();
}
closeFile();
return start;
}
public void printValues() throws Exception {
while (input.hasNextLine()) {
String str = input.nextLine();
System.out.println(str);
}
throw new Exception ("Test for StackTrace");
}
}