Skip to content

Instantly share code, notes, and snippets.

@MaikKlein
Last active May 16, 2018 16:08
Show Gist options
  • Save MaikKlein/7b52c9241e54e2e1decb8f0446c0c906 to your computer and use it in GitHub Desktop.
Save MaikKlein/7b52c9241e54e2e1decb8f0446c0c906 to your computer and use it in GitHub Desktop.
import java.lang.Math; // headers MUST be above the first class
import java.util.ArrayList;
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
Persons p = new Persons("John;27;\nPeter;22;");
// Change peters age to 30
p.persons.get(1).age = 30;
// Print the result
System.out.println(p.save());
}
}
// you can add other public classes to this editor in any order
public class Person
{
public String name;
public int age;
public Person(String line){
String[] content = line.split(";");
name = content[0];
age = Integer.parseInt(content[1]);
}
}
public 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