Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By gimbal2

Thread: How to pass info to the constructor from a text file?

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default How to pass info to the constructor from a text file?

    Suppose there're two classes: Exam and MainExam (contains a main method). class Exam has a constructor public Exam(String firstName, String lastName, int ID). Class MainExam reads data from a textfile. For example, the data can be: John Douglas 57. How can one pass data to the constructor from a textfile?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to pass info to the constructor from a text file?

    Read the data into three variables and use them in the constructor?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to pass info to the constructor from a text file?

    Quote Originally Posted by ssk2659 View Post
    How can one pass data to the constructor from a textfile?
    One has to first learn how to read data from a text file. Apparently you were already playing around with the Scanner class:

    http://www.java-forums.org/new-java/...er-string.html

    Probably to get input from the command prompt. Now you need to go learn how to use the Scanner class to read data from a file.
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to pass info to the constructor from a text file?

    Maybe I was taking "Class MainExam reads data from a textfile." too literally.
    It read to me like they were already reading from the file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to pass info to the constructor from a text file?

    I interpreted it as a homework assignment where you should read "the code should be doing this". But that's what you get when you ask minimalistic questions.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How do I pass information to a constructor?
    By Ormolu611 in forum New To Java
    Replies: 2
    Last Post: 02-24-2013, 06:33 PM
  2. how to pass the info to another applet?
    By nananya in forum Java Applets
    Replies: 7
    Last Post: 12-12-2011, 04:23 PM
  3. Replies: 1
    Last Post: 01-30-2011, 09:05 AM
  4. [SOLVED] Java not fill in all info for text file
    By gotenks05 in forum New To Java
    Replies: 0
    Last Post: 05-24-2009, 05:17 PM
  5. Replies: 16
    Last Post: 05-08-2008, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •