Guest User

Untitled

a guest
May 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4. class HelloWorld
  5. {
  6. public static void main(String[] args)
  7. {
  8. String input = "John;27;\nPeter;22;";
  9. System.out.println("Input:");
  10. System.out.println(input);
  11. System.out.println("");
  12. Persons p = new Persons(input);
  13. // Change peters age to 30
  14. p.persons.get(1).age = 30;
  15. // Print the result
  16. System.out.println("Output:");
  17. System.out.println(p.save());
  18.  
  19. }
  20. }
  21.  
  22. class Person
  23. {
  24. public String name;
  25. public int age;
  26.  
  27. public Person(String line){
  28. String[] content = line.split(";");
  29. name = content[0];
  30. age = Integer.parseInt(content[1]);
  31. }
  32. }
  33.  
  34. class Persons {
  35. public ArrayList<Person> persons;
  36.  
  37. public Persons(String file){
  38. ArrayList<Person> persons = new ArrayList<Person>();
  39. String[] lines = file.split("\\r?\\n");
  40. for (String line : lines) {
  41. Person p = new Person(line);
  42. persons.add(p);
  43. }
  44. this.persons = persons;
  45. }
  46.  
  47. public String save() {
  48. String s = "";
  49. for (Person person : persons) {
  50. s += person.name + ";" + person.age + ";\n";
  51. }
  52. return s;
  53. }
  54. }
Add Comment
Please, Sign In to add comment