Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Read a data from a text file and create an object from these data in this text file

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ??


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Read a data from a text file and create an object from these data in this text fi

    What have you tried?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read a data from a text file and create an object from these data in this text fi

    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...
    Last edited by copeg; March 25th, 2011 at 11:05 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 View Post
    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

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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?
    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 )

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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..

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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
    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)

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ?

Similar Threads

  1. Carving data from text file based on expressions
    By fortune2k in forum Paid Java Projects
    Replies: 1
    Last Post: March 4th, 2011, 05:07 PM
  2. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  3. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM