Page 1 of 3 123 LastLast
Results 1 to 20 of 41
Like Tree1Likes

Thread: Importing from text file and creating objects from the text files !??

  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Importing from text file and creating objects from the text files !??

    Hello,

    i've been trying to write a program for some time now, but im encountering problems while trying to complete it. The program has a student class and a course class (set up with some info about the class, like #, professor name, course title, course time). Now, i have a text file in the workspace, and i have to import the data from the textfile, and thats just what i did, but then there is an option which allows the user to delete a course only by inputing the course number, and when he does that, the program outputs the course's name, and confirms the deletion of the course (From the student's record, i created a vector for that and imported all the courses from the text file). But how can i let the program know what's the name of the course when the user inputs the course number ???

    PS: When the data is read from the file, objects should be created and added to the student's course record. <- i think here's where i messed up ? i imported the data, but how can i actually make them objects before adding them into the vector ?

    PHP Code:
    public static void main(String[] args) throws IOException
    	{
    		Scanner scan = new Scanner(System.in);
    		Vector Record = new Vector();
    		
    	try {FileReader filereader = new FileReader("CLASSES.txt");
    	     BufferedReader bufferedreader = new BufferedReader(filereader);
    	     String test = "";
    	     while(test != null)
    	     {
    	    	 Record.addElement(test);
    	    	 test = bufferedreader.readLine();
    	     }
    Last edited by toiletpaper; 05-25-2014 at 01:25 PM.

  2. #2
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Here's a sample of a course in the text file (it contains 7 courses):

    BS(stands for business)|201|Intr. to Marketing|MWF|01:00pm-1:50pm|Peter Morok
    BS|202|Advanced Marketing|TR|3:00pm-4:30pm|Sarah Grey

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Importing from text file and creating objects from the text files !??

    how can i actually make them objects before adding them into the vector ?
    Create an instance of the class with the new statement. Pass the data to be saved in the class in the class's constructor.
    Java Code:
    data1 = ...     //  get the data from the user or the file
    data2 = ...
    YourClass yrCls = new YourClass(data1, data2); // create instance and pass data via constructor
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Importing from text file and creating objects from the text files !??

    You are reading in the lines of text into a String and then adding them to a vector. Since String is an object, that answers your last question. However, perhaps what is expected is for you to parse each line and retrieve each field between the vertical bars. Then store them in some class. That way, you would be better prepares to find the appropriate record when required.

    Also, I presume the scanner would be used latter. To avoid clutter I would suggest you just focus on reading in the list, coverting to some class if required, and then printing it out. Don't add any other code until you get that working. Then you can focus on deleting specific records.


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by jim829 View Post
    You are reading in the lines of text into a String and then adding them to a vector. Since String is an object, that answers your last question. However, perhaps what is expected is for you to parse each line and retrieve each field between the vertical bars. Then store them in some class. That way, you would be better prepares to find the appropriate record when required.

    Also, I presume the scanner would be used latter. To avoid clutter I would suggest you just focus on reading in the list, coverting to some class if required, and then printing it out. Don't add any other code until you get that working. Then you can focus on deleting specific records.


    Regards,
    Jim

    Actually every line should be stored in 1 empty field in the Vector, but after the data are read from the file, objects should be created and added to the student's record. What i did, was i imported the data from the text file and added it to the Vector, which is wrong i guess... i have to convert them into objects, but i have no idea how to... im still trying to figure it out.

    Any help would be greatly appreciated !

    Thanks!
    regards,
    toiletpaper

  6. #6
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by Norm View Post
    Create an instance of the class with the new statement. Pass the data to be saved in the class in the class's constructor.
    Java Code:
    data1 = ...     //  get the data from the user or the file
    data2 = ...
    YourClass yrCls = new YourClass(data1, data2); // create instance and pass data via constructor
    okay i tried the following:

    create a new object for a course

    PHP Code:
    Course BS_201 = new Course(201, 3, "BS", "Peter Morok", "02:00pm-03:15pm");
    Then in order for the user to delete a course i wrote the following:

    PHP Code:
    System.out.println("Enter course number you want to delete: ");
    	    	 int course_number = scan.nextInt();
    	    	 System.out.println("Confirm deletion: " + {THIS SHOULD INCLUDE COURSE NAME});
    How do i achieve this ? i need some kind of bond between the coursenumber and course name, to able to retrieve course name from the user's input (course number)
    Last edited by toiletpaper; 05-25-2014 at 03:37 PM.

  7. #7
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Its actually wrong to write the object myself, because the imported text file lines should be converted into objects of the class "Course" before being added into the vector how do i achieve that ?
    Last edited by toiletpaper; 05-25-2014 at 03:52 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Importing from text file and creating objects from the text files !??

    wrong to write the object
    Can you explain what that means?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Course BS_201 = new Course(201, 3, "BS", "Peter Morok", "02:00pm-03:15pm"); Im creating the object myself here, however the program should read the data from the file and create objects that are added into the student's records.

    If i were to write the objects myself, then what's the use of importing the courses from the text file in the first place ? Correct me if im wrong, im kind of confused here, and im stuck in writing this prog

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by toiletpaper View Post
    Course BS_201 = new Course(201, 3, "BS", "Peter Morok", "02:00pm-03:15pm"); Im creating the object myself here, however the program should read the data from the file and create objects that are added into the student's records.

    If i were to write the objects myself, then what's the use of importing the courses from the text file in the first place ? Correct me if im wrong, im kind of confused here, and im stuck in writing this prog

    Edit: I just realized what you are talking about. You must read in the data as a string and the split the string programmatically between the vertical bars. Then you use those "pieces" of code to construct you objects. You don't need to hand code each object. Look at the String.split() method.

    Regards,
    Jim
    Last edited by jim829; 05-25-2014 at 04:13 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by jim829 View Post
    Edit: I just realized what you are talking about. You must read in the data as a string and the split the string programmatically between the vertical bars. Then you use those "pieces" of code to construct you objects. You don't need to hand code each object.

    Regards,
    Jim
    Yea, how do i do that i need some help...

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Importing from text file and creating objects from the text files !??

    I edited the code while you were replying. Try this as an example:

    String str = "A|B|C";

    Now check out the String.split() method to split the value between the vertical bars.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by jim829 View Post
    I edited the code while you were replying. Try this as an example:

    String str = "A|B|C";

    Now check out the String.split() method to split the value between the vertical bars.

    Regards,
    Jim
    Thanks for the hint mate, im going to try that, ill return if any problems pop up.

  14. #14
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Hm... I still don't get why should i split them apart ?? i just split them apart, but how can i know what's the course's name when the user inputs the course's number in order to delete it ?? There's NO relationship between them, especially if i split them apart... this is really frustrating!!

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by toiletpaper View Post
    Hm... I still don't get why should i split them apart ?? i just split them apart, but how can i know what's the course's name when the user inputs the course's number in order to delete it ?? There's NO relationship between them, especially if i split them apart... this is really frustrating!!
    But you use the pieces as arguments to the class above to populate your class Course. Your can store each class in an array or hash map. Later when you want to prompt the user to delete a class, you just iterate thru the courses and look for that class based on one of the fields. You don't need to know if the user typed it in correctly or not. Either they did and the course did or did not exist or there was a typo.

    If you are only going to delete a course based on number then you may want to read up on hash maps. They could be useful here.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Importing from text file and creating objects from the text files !??

    when you read a line a string, the whole line is big string. You need to split it to separate parts to get the correct value for each field of your objects.
    The split() method can take a regular expression, construct one to split them according to the vertical lines.
    You will receive an array of strings and you can know depending on the order of the values in the text file that the [0] stands for the department for example

  17. #17
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Quote Originally Posted by jim829 View Post
    But you use the pieces as arguments to the class above to populate your class Course. Your can store each class in an array or hash map. Later when you want to prompt the user to delete a class, you just iterate thru the courses and look for that class based on one of the fields. You don't need to know if the user typed it in correctly or not. Either they did and the course did or did not exist or there was a typo.

    If you are only going to delete a course based on number then you may want to read up on hash maps. They could be useful here.

    Regards,
    Jim

    Do you happen to have a skype account ? i could rly use some help over teamviewer or so, if you're okay with it. nothing i try is actually working. I either get some errors, or end up doing something completely different from what is expected from the program.

  18. #18
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Importing from text file and creating objects from the text files !??

    No Skype. And we prefer to do this on the forum so others may benefit. You need to post what you have and we can go from there. But the general idea is:

    Java Code:
    String s = "< input text >";
    String fields[] = s.split("\\|");
    Course course = new Course(fields[0], fields[1],...);
    Edit: Changed "|" to "\\|" in split method.

    Regards,
    Jim
    Last edited by jim829; 05-25-2014 at 06:03 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Importing from text file and creating objects from the text files !??

    regex can be tricky to use. Some characters are regex control characters. To use a control character as the delimiter it must be escaped by putting a \ before it: "\|"
    And then you need to escape the \ for the java compiler by adding another \: "\\|"
    Java Code:
    String fields[] = s.split("\\|");  // split into fields at the |s
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Importing from text file and creating objects from the text files !??

    Edit: Tokenizer worked, i moved all the elements into another vector (will be posting it when i finish it)
    Last edited by toiletpaper; 05-25-2014 at 06:55 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2012, 05:31 AM
  2. Replies: 1
    Last Post: 06-04-2012, 05:01 AM
  3. read from text file into array of objects
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 12:40 PM
  4. Creating Ouput Text File
    By Raian in forum New To Java
    Replies: 14
    Last Post: 05-24-2010, 09:56 AM
  5. creating a graph from a text file
    By chinolee in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 08:36 AM

Posting Permissions

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