class Person
{
private String imie;
private String nazwisko;
private String plec;
private int wiek;
public Person(String _imie, String _nazwisko, String _plec, int _wiek)
{
this.imie = _imie;
this.nazwisko = _nazwisko;
this.plec = _plec;
this.wiek = _wiek;
}
public String toString(){
return this.imie + " " +this.nazwisko;
}
public static void main(String[] args)
{
try {
File f = new File("lista.txt");
Scanner sc = new Scanner(f);
List<Person> people = new ArrayList<Person>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] dane = line.split(":");
String imie = dane[0];
String nazwisko = dane[1];
String plec = dane[2];
int wiek = Integer.parseInt(dane[3]);
Person p = new Person(imie, nazwisko, plec, wiek);
people.add(p);
}
//for (int i=0;i<people.size();i++){
for(Person p: people){
if (p.wiek > 18)
{
System.out.println(p.toString());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Zrobiłem to zgodnie z tym co pisal Shalom, przerobiłem oczywiście troszkę program, zamiast atrybutu punkty wrzuciłem płeć. Udało mi się wyświetlić osoby pełnoletnie. Mam jednak problem z tym w jaki sposób zsumować np. wiek wszystkich osób z listy. z gory dzieki
}