I wrote a program that creates 3 files with .txt format and writes some data's into them. Now I am asked to define a value object for these files, And override the toString method, And then when I read the files Set them in to Value objects. Now I'm completely confused and do not know where to start. Thank you in advance for your help
-
2Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Federico klez Culloca Mar 09 '21 at 13:54
-
2@Dren your heart is in the right place, but you don't need to be aggressive about it, especially with new users. There are more constructive ways to point them in the right direction. – Federico klez Culloca Mar 09 '21 at 13:55
-
@Dren Of course not! The problem is that I do not know where to start, Although I searched a lot – Pourya Siyami Mar 09 '21 at 13:56
-
1@PouryaSiyami if you tell us what is the content of the file we can help you then. – Dren Mar 09 '21 at 13:57
-
2@FedericoklezCulloca Thank you, I will keep that in mind. – Dren Mar 09 '21 at 13:58
-
What is a value object? – NomadMaker Mar 09 '21 at 14:02
-
@PouryaSiyami Based on your question although without seeing what the context of the file is it is difficult to give a correct answer, but I assumed a case where you will create objects from the file, so see if you have any questions you can ask me. – Dren Mar 09 '21 at 14:27
-
1@Dren Thanks for taking the time to respond, Yes, your answer was very close to what I wanted. – Pourya Siyami Mar 09 '21 at 14:47
1 Answers
If you want to create objects from text files first what are you going to do is read the file. There are many available classes in the Java API that can be used to read files: BufferedReader
, FileReader
etc.
The number of objects you will create will depend on the number of lines in the file. For example if you have a file with the following content
PersonName1,PersonSurname1,20
PersonName2,PersonSurname2,22
PersonName3,PersonSurname3,23
Let's say that these lines represent name, surname, and age of some users
What you need to do first is create a class that will represent this object, as we see in the text file the object will contain three parameters , so you create a class with three atribbutes
public class Person {
private String name;
private String surname;
private Integer age;
public Person(String name, String surname, Integer age) {
this.name = name;
this.surname = surname;
this.age = age;
}
@Override
public String toString() {
return "NAME : " + name + " : " + surname;
}
Now comes the reading part , for reading the file we will use BufferedReader
, we read every line of the file by splitting the line by commas(,) for every iteration
String [] parameters = line.split(",");
This array of String now will represent our user attributes so we access it
String name = parameters[0];
String surname = parameters[1];
Integer age = Integer.parseInt(parameters[2]);
And now we create an object of Person every iteration and we add it to the list
Person p = new Person(name,surname,age);
list.add(p);
Example
BufferedReader bufferedReader = new BufferedReader(new FileReader("test.txt"));
List<Person> list = new ArrayList<>();
String line = null;
while((line = bufferedReader.readLine())!=null){
try {
String[] parameters = line.split(",");
if(parameters.length == 3) {
String name = parameters[0];
String surname = parameters[1];
Integer age = Integer.parseInt(parameters[2]);
Person p = new Person(name, surname, age);
list.add(p);
}
}catch (NumberFormatException e){
}
}
System.out.println("Object created from the file");
list.stream().forEach(System.out::println);
OUTPUT
NAME : PersonName1 : PersonSurname1
NAME : PersonName2 : PersonSurname2
NAME : PersonName3 : PersonSurname3

- 1,239
- 1
- 8
- 17