Question

Exercise 1 — Input information then write to a file Write an application that will have...

Exercise 1 — Input information then write to a file

Write an application that will have someone input any number of friends' phone data and write the data to a file.

File Details

The output file should be named "friend1.txt" The data in the files will be last name, first name, age and phone number, each piece of data on a separate line, such as:

Smith

Pete
15

888-1234

Jones

Sue

17

885-4578

Use parallel arrays to create this information before writing the output file.

Please help code this in Java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package Oct2_2020;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class WriteToFile {
        public static void main(String[] args) throws Exception{
                Scanner sc = new Scanner(System.in);
                System.out.print("Enter number of friends: ");
                int n = sc.nextInt();
                String firstNames[] = new String[n];
                String lastNames[] = new String[n];
                int ages[]=new int[n];
                String phones [] = new String[n];
                PrintWriter pw = new PrintWriter(new FileWriter("frind1.txt"));
                for(int i=0;i<n;i++) {
                        System.out.println("Enter lastName: ");
                        lastNames[i]=sc.next();
                        System.out.print("Enter firstName: ");
                        firstNames[i]=sc.next();
                        System.out.print("Enter age: ");
                        ages[i]=sc.nextInt();
                        System.out.println("Enter phone: ");
                        phones[i]=sc.next(); 
                }
                for(int i=0;i<n;i++) {
                        pw.println(lastNames[i]);
                        pw.println(firstNames[i]);
                        pw.println(ages[i]);
                        pw.println(phones[i]);
                }
                pw.close();
        }
}

frind 1.txt X 1 Smith 2 pete 3 15 4 888-1234 5 Jones 6 Sue 7 17 8885-4578 9

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Exercise 1 — Input information then write to a file Write an application that will have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT