Created
August 7, 2015 07:28
-
-
Save alexshavelev/1db424a46b9d41d72a02 to your computer and use it in GitHub Desktop.
level19.lesson10.home03;
This file contains 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
package com.javarush.test.level19.lesson10.home03; | |
import java.util.Date; | |
public class Person { | |
private String name; | |
private Date birthday; | |
public Person(String name, Date birthday) { | |
this.name = name; | |
this.birthday = birthday; | |
} | |
public String getName() { | |
return name; | |
} | |
public Date getBirthday() { | |
return birthday; | |
} | |
} |
This file contains 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
package com.javarush.test.level19.lesson10.home03; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
/* Хуан Хуанович | |
В метод main первым параметром приходит имя файла. | |
В этом файле каждая строка имеет следующий вид: | |
имя день месяц год | |
где [имя] - может состоять из нескольких слов, разделенных пробелами, и имеет тип String | |
[день] - int, [месяц] - int, [год] - int | |
данные разделены пробелами | |
Заполнить список PEOPLE импользуя данные из файла | |
Закрыть потоки. Не использовать try-with-resources | |
Пример входного файла: | |
Иванов Иван Иванович 31 12 1987 | |
Вася 15 5 2013 | |
*/ | |
public class Solution { | |
public static final List<Person> PEOPLE = new ArrayList<Person>(); | |
public static void main(String[] args) throws IOException | |
{ | |
// FileReader fileReader = new FileReader(args[0]); | |
FileReader fileReader = new FileReader("/home/user/java_test/test2"); | |
BufferedReader rd = new BufferedReader(fileReader); | |
String line; | |
String[] symbols; | |
int sizeSymbols; | |
String personName; | |
Date personDate; | |
while ((line = rd.readLine()) != null) { | |
symbols = line.split(" "); | |
sizeSymbols = symbols.length; | |
personDate = new Date(Integer.parseInt(symbols[sizeSymbols - 1]), Integer.parseInt(symbols[sizeSymbols - 2]), Integer.parseInt(symbols[sizeSymbols - 3])); | |
personName = getName(symbols, sizeSymbols - 4); | |
System.out.println(personName + " " + personDate); | |
PEOPLE.add(new Person(personName, personDate)); | |
} | |
rd.close(); | |
fileReader.close(); | |
} | |
public static String getName(String[] args, int index) { | |
StringBuilder res = new StringBuilder(); | |
for (int i = 0; i <= index; i++) { | |
res.append(args[i]).append(" "); | |
} | |
return res.toString(); | |
} | |
} |
Как по мне с месяцем решение правильное.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Не правильное решение. ошиблись с месяцем
вместо
personDate = new Date(Integer.parseInt(symbols[sizeSymbols - 1]), Integer.parseInt(symbols[sizeSymbols - 2]), Integer.parseInt(symbols[sizeSymbols - 3]));
нужно
personDate = new Date(Integer.parseInt(symbols[sizeSymbols - 1]), Integer.parseInt(symbols[sizeSymbols - 2])-1, Integer.parseInt(symbols[sizeSymbols - 3]));