Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- class HelloWorld
- {
- public static void main(String[] args)
- {
- String input = "John;27;\nPeter;22;";
- System.out.println("Input:");
- System.out.println(input);
- System.out.println("");
- Persons p = new Persons(input);
- // Change peters age to 30
- p.persons.get(1).age = 30;
- // Print the result
- System.out.println("Output:");
- System.out.println(p.save());
- }
- }
- class Person
- {
- public String name;
- public int age;
- public Person(String line){
- String[] content = line.split(";");
- name = content[0];
- age = Integer.parseInt(content[1]);
- }
- }
- class Persons {
- public ArrayList<Person> persons;
- public Persons(String file){
- ArrayList<Person> persons = new ArrayList<Person>();
- String[] lines = file.split("\\r?\\n");
- for (String line : lines) {
- Person p = new Person(line);
- persons.add(p);
- }
- this.persons = persons;
- }
- public String save() {
- String s = "";
- for (Person person : persons) {
- s += person.name + ";" + person.age + ";\n";
- }
- return s;
- }
- }
Add Comment
Please, Sign In to add comment