从txt文件中存储带有空格的数据,使用readLine。

huangapple go评论80阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2020年9月30日 17:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64135087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定