Last active
May 16, 2018 16:21
-
-
Save MaikKlein/71c51f57e80a769255dfe97891be9bce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment