Read a data from a text file and create an object from these data in this text file
Hi all.. i have a text file it names "person.txt" ...
This is content of person.txt :
***********
m 25 68
g 35 55
g 23 65
m 19 56
m 40 75
***********
if first line is "m" i should create man object, if first line is "g" i should create girl object... other lines show me respectively age and weight.
How can i slove this problem ??
Re: Read a data from a text file and create an object from these data in this text fi
Re: Read a data from a text file and create an object from these data in this text fi
Code java:
try {
InputStream fis = new FileInputStream("person.txt");
Scanner scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] pieces = line.split(" ");
if(pieces == line.split("m ")){
create man obj.
}
if(pieces == line.split("g ")){
create girl obj.
}
}
scanner.close();
} catch (Exception e) {
System.out.println("There has been some error.");
}
like this but if not correctly and my think is not correct..
i couldn't find how can i do...
Re: Read a data from a text file and create an object from these data in this text fi
Please use the code tags (see my signature for instructions) - I've edited your post to use them.
I'd suggest you read the following 2 links:
1) Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
2) http://www.javaprogrammingforums.com...html#post18725
Re: Read a data from a text file and create an object from these data in this text fi
My question how can i read the data from a text file?
your answer not relited my question :( but thanks.
Quote:
Originally Posted by
copeg
Re: Read a data from a text file and create an object from these data in this text fi
Quote:
My question how can i read the data from a text file?
Really? I honestly didn't know what the question was, and was addressing your "create man obj" notes in the source code you posted - seems to me your code reads the file just fine, unless there are errors or exceptions that you did not post. Perhaps you need to state your question more specifically (see How To Ask Questions The Smart Way )
Re: Read a data from a text file and create an object from these data in this text fi
i don't know how can i create an object in the text file.. How can i get line by line informations from the text file.. These are important if first line is "m" i must be create man object or first line is "g" i must be create girl object these informations..
Re: Read a data from a text file and create an object from these data in this text fi
I think I understand your question...consider the following
Code :
String line = "m 25 68";
String[] pieces = line.split(" ");
///pieces is an array, pieces [0] is "m", pieces [1] is "25", etc...
Just use the appropriate index of the array to check if it is an "m" or "g". If you don't understand arrays, see Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Read a data from a text file and create an object from these data in this text fi
Well.. You're right but i don't know the content in text file because of that i can't use
<JAVA>String line = "m 25 68";</JAVA>
If i know.. and text file have 10.000 person what can i do ?