Basically I need help creating a method that will save the following variables t
ID: 3639569 • Letter: B
Question
Basically I need help creating a method that will save the following variables to a .txt file using a linked list data structure.Name - Text Field
Age - Text Field
Gender - Drop down Combobox
Email - Text Field
Username - Text Field
Password - Password Field
These items are on a GUI that I already created, I am not going to paste the code in here because there's too much. However once the user enters the data into text fields or chooses from a drop down combobox they click a button that will initialize the method to save all of this to a text file.
Explanation / Answer
import java.util.LinkedList;
import java.io.*;
public class LinkedAccountList {
public LinkedAccountList() {
writeToFile(createList());
}
public void writeToFile(LinkedList acct) {
String file_name = "output.txt";
File file = new File(file_name);
try {
boolean exist = file.createNewFile();
if (!exist) {
System.out.println("File already exists.");
System.exit(0);
} else {
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
for (Account element : acct) {
out.write(element.name + " " + element.age + " "
+ element.email + " " + element.gender + " "
+ element.username + " " + element.password + " ");
}
out.close();
System.out.println("File created successfully.");
}
} catch (Exception e) {
}
}
public LinkedList createList() {
LinkedList ml = new LinkedList();
ml.add(new Account("Gary", "21", "Male", "gary@aol.com", "gary12","gary123"));
ml.add(new Account("Sally", "21", "Female", "sally@aol.com", "sally12","sally123"));
for (Account element : ml) {
System.out.println(element + " ");
}
return ml;
}
private class Account {
private String name;
private String age;
private String gender;
private String email;
private String username;
private String password;
Account(String n, String a, String g, String e, String u, String p) {
name = n;
age = a;
gender = g;
email = e;
username = u;
password = p;
}
public String toString() {
return name + " " + age + " " + gender + " " + email + " "+ username;
}
}
public static void main(String args[]) {
LinkedAccountList p = new LinkedAccountList();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.