0% found this document useful (0 votes)
75 views2 pages

Programming Activity # 7 - Arrays and File Handling

Performance Task: Create a Java program that will create a list of contacts of 10 persons. Name your text file with “phonebook.txt” that contains data items to be stored: name, birthday, gender, age, and contact number. The program allows the user to input data and store it in a "phonebook.txt" file, and read the data from the text.

Uploaded by

Nat Nat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views2 pages

Programming Activity # 7 - Arrays and File Handling

Performance Task: Create a Java program that will create a list of contacts of 10 persons. Name your text file with “phonebook.txt” that contains data items to be stored: name, birthday, gender, age, and contact number. The program allows the user to input data and store it in a "phonebook.txt" file, and read the data from the text.

Uploaded by

Nat Nat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Nathz C.

Paloma CCE103/L (6169) 01/25/22

Activity#7

Instructions: Copy the codes and screenshots of the result into an MS Word file and name it using
your last name. Convert your file from MS Word to PDF format before uploading it to this assignment
page. Attached below is a file containing sample codes for writing and reading data using a file.

Performance Task: Create a program that will create a list of contacts of 10 persons. Name your
text file with “phonebook.txt” that contains data items to be stored: name, birthday, gender, age, and
contact number. The program allows the user to input data and store it in a "phonebook.txt" file, and
read the data from the text.

package Activity7;
import java.io.*;
import java.util.Scanner;

public class Paloma_Activity7 {


public static void main(String[] args) throws IOException {
String fileName = "phonebook.txt";
String[] name = new String[10];
String[] birthday = new String[10];
String[] gender = new String[10];
int[] age = new int[10];
String[] contactNumber = new String[10];

Scanner input = new Scanner(System.in);

for (int i = 0; i < 10; i++) {


System.out.print("Enter name: ");
name[i] = input.nextLine();
System.out.print("Enter birthday: ");
birthday[i] = input.nextLine();
System.out.print("Enter gender: ");
gender[i] = input.nextLine();
System.out.print("Enter age: ");
age[i] = input.nextInt();
input.nextLine();
System.out.print("Enter contact number: ");
contactNumber[i] = input.nextLine();
}
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < 10; i++) {
bw.write(name[i] + "," + birthday[i] + "," + gender[i] + "," + age[i] + "," + contactNumber[i]);
bw.newLine();
}
bw.close();

FileReader fr = new FileReader(fileName);


BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}

You might also like