How can I produce this program using a text file being read with an ArrayList incorporated?
example of a text file: the goal is to produce 25 contacts
Kent Brockman 12345(zip) 123 Hope St(address) Brooklyn(city) NY(state) 2014567894(#) Charles Burns 24923(zip) 123 Fast St Harlem NY 2014567456
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class ReadPersons { static class Person { private String firstName; private String lastName; private int zip; private String address; private String city; private String state; private String phone; public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getZip() { return this.zip; } public void setZip(int zip) { this.zip = zip; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getState() { return this.state; } public void setState(String state) { this.state = state; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String toString() { return "firstName: " + firstName + ", " + "lastName: " + lastName + ", " + "zip: " + zip + ", " + "address: " + address + ", " + "city: " + city + ", " + "state: " + state + ", " + "phone: " + phone; } } public static void main(String[] args) throws FileNotFoundException { String fileName = "persons.txt"; // change it here. Scanner reader = new Scanner(new File(fileName)); // to store the data ead from the file ArrayList<Person> persons = new ArrayList<>(); // now read the file one line at a time String line; while (reader.hasNextLine()) { line = reader.nextLine(); String tokens[] = line.split("\t"); Person p = new Person(); p.setFirstName(tokens[0]); p.setLastName(tokens[1]); p.setZip(Integer.parseInt(tokens[2])); p.setAddress(tokens[3]); p.setCity(tokens[4]); p.setState(tokens[5]); p.setPhone(tokens[6]); // add the person into arraylist persons.add(p); } // Now print the list. for (Person p : persons) { System.out.println(p); } reader.close(); } }
************************************************** persons.txt: Kent Brockman 12345 123 Hope St Brooklyn NY 2014567894 Charles Burns 24923 123 Fast St Harlem NY 2014567456 Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.