Java Create a text file named lnfotxt Enter the following in

Java
Create a text file named \"lnfo.txt\". Enter the following information using Linked List (any method). Name rightarrow String Address rightarrow String Phone rightarrow String There should be continues loop which will ask the above information and write down in the TEXT File. When user inputs Exit in Name Field then program should be ended.

Solution

import java.util.*;
import java.io.*;
public class Info{
    public static void main(String[] args) {
        LinkedList<User> users = new LinkedList<User>();
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.print(\"Name: \");
        user.name = sc.nextLine();
        while (!user.name.equals(\"Exit\")){
            System.out.print(\"Address: \");
            user.address = sc.nextLine();
            System.out.print(\"Phone: \");
            user.phone = sc.nextLine();
            users.add(user);
            user = new User();
            System.out.print(\"Name: \");
            user.name = sc.nextLine();
        }
        sc.close();

        try{
            File file = new File(\"Info.txt\");
           if (!file.exists()) {
               file.createNewFile();
           }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
            for (User u: users){
                String s = u.name + \"\\t\" + u.address + \"\\t\" + u.phone + \"\ \";
                bw.write(s);
            }
            bw.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
class User{
    public String name;
    public String address;
    public String phone;
}

Java Create a text file named \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site