英文:
Storing data from txt file with whitespaces with readLine
问题
try
{
File file = new File(prop.getProperty("path"));
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String name, lastName, mail;
Date birthDate;
String line;
while ((line = br.readLine()) != null) {
if (!line.trim().equals("")) {
name = br.readLine();
lastName = br.readLine();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
birthDate = format.parse(br.readLine());
mail = br.readLine();
Contacto contacto = new Contacto(name, lastName, birthDate, mail);
gestor.addContact(contacto);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String birth;
while ((name = br.readLine()) != null && (lastName = br.readLine()) != null && (birth = br.readLine()) != null && (mail = br.readLine()) != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
birthDate = format.parse(birth);
Contacto contacto = new Contacto(name, lastName, birthDate, mail);
gestor.addContact(contacto);
}
文本文件的结构如下:
姓名
姓氏1 姓氏2
1970-01-01
sample@mail.es
英文:
I'm trying to write a code that reads from a txt information about a person and stores it in variables, then uses the variables to create a contact and then add that contact to an ArrayList. When I execute the program it says that it can't parse the email, like its trying to store the email as if it was the date. Is there any way to avoid this?
try
{
File file = new File(prop.getProperty("path"));
FileReader fr = new FileReader (file);
BufferedReader br = new BufferedReader(fr);
String name, lastName, mail;
Date birthDate;
String line;
while((line = br.readLine())!=null)
{
if (!line.trim().equals(""))
{
name = br.readLine();
lastName = br.readLine();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
birthDate = format.parse(br.readLine());
mail = br.readLine();
Contacto contacto = new Contacto(name, lastName, birthDate, mail);
gestor.addContact(contacto);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
Ive also tried this, but it stores the contact twice:
String birth;
while((name = br.readLine())!= null && (lastName = br.readLine())!= null && (birth = br.readLine())!= null && (mail = br.readLine())!= null)
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
birthDate = format.parse(birth);
Contacto contacto = new Contacto(name, lastName, birthDate, mail);
gestor.addContact(contacto);
}
The txt file is structured like this:
Name
LastName1 LastName2
1970-01-01
sample@mail.es
答案1
得分: 0
try {
File file = new File("/Users/soumyabratakole/test/test");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
String name = null;
String lastName1 = null;
String lastName2 = null;
Date birthDate = null;
String email = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int i = 0;
while ((line = br.readLine()) != null) {
if (!line.trim().equals("")) {
int j = i % 5;
i++;
switch (j) {
case 0:
name = line;
break;
case 1:
lastName1 = line;
break;
case 2:
lastName2 = line;
break;
case 3:
birthDate = format.parse(line);
break;
case 4:
email = line;
break;
}
if (j == 4) {
System.out.println("Name: " + name + ", Lastname " + lastName1 + " " + lastName2 + ", BDay " + birthDate + ", Email " + email);
// Other operations
Contacto contacto = new Contacto(name, lastName1, birthDate, email);
gestor.addContact(contacto);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
英文:
It can be something like :
try {
File file = new File("/Users/soumyabratakole/test/test");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
String name=null;
String lastName1=null;
String lastName2=null;
Date birthDate=null;
String email=null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int i=0;
while ((line = br.readLine()) != null) {
if (!line.trim().equals("")){
int j =i%5;
i ++;
switch (j){
case 0:
name = line;
break;
case 1:
lastName1 = line;
break;
case 2:
lastName2 = line;
break;
case 3:
birthDate = format.parse(line);
break;
case 4:
email = line;
break;
}
if (j == 4){
System.out.println("Name: " + name + ", Lastname " + lastName1 + " " + lastName2 + ", BDay " + birthDate + ", Email " + email);
//Other operations
Contacto contacto = new Contacto(name, lastName1, birthDate, mail);
gestor.addContact(contacto);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论