• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Print strings from a file and select from a menu

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I am running into a problem in a program I am working on. It IS an assignment so I will have to ask that noone give me code that I can just copy and paste. I just need some direction. I am trying to have a student add a course to their file(a text file). I have created a new blank text file(using .createnewfile) for this student and created a file object and scanner for the file with the course. The file(course_list.txt) is as follows:

Course Name, Course Id, Time, Introduction to Philosphy, Phil101, Mon 2:00p-4:00p Wed 9:00a-11:00a Thu 10:00a-12:00p
Introduction to Programming 2, Prog201, Wed 9:00a-11:00a Thu 10:00a-12:00p Fri 1:00p-3:00p
Meaning of Life, Phil411, Mon 9:00a-12:00p Tue 4:00p-7:00p
Introduction to Administrative Studies, Adms101, Tue 12:00p-2:00p Wed 12:00p-2:00p Thu 12:00p-2:00p

end of file

Now I am thinking I should print a numbered list of the courses and use a switch statement to let the user choose which course to add. How I am going to get the numbers(option 1, option 2, etc) in front of the strings, or even get them out of the file as seperate entities, I am not sure. As you can see in the code below(the method at the very bottom), I am familiar with reading text from a file. But now it is getting a but more complex, and I am not sure how to handle it properly.

sample output(preferably without commas as the delimiter):
Course Name, Course Id, Time
1. Introduction to Philosphy, Phil101, Mon 2:00p-4:00p Wed 9:00a-11:00a Thu 10:00a-12:00p
2. Introduction to Programming 2, Prog201, Wed 9:00a-11:00a Thu 10:00a-12:00p Fri 1:00p-3:00p
etc etc.

I also have to make sure the student cannot add a course if the times of day conflict with a course that they have already added.. I guess I could just search the file line by line, assuming everything is on one line. Which it isn't.

Here is the code so far
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I also have to make sure the student cannot add a file if the times conflict on the same day.


You meant to add a record to a file? Can you elaborate this a little more?

How I am going to stick numbers in front of them, or even get them out of the file seperately I am not sure.


You want to display some numbers (1,2,3....) while displaying the records or...?

...ask that no1 give...


Please UseRealWords when posting, it make confuses among the readers...
 
Erik Gruber
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:

But I also have to make sure the student cannot add a file if the times conflict on the same day.


You meant to add a record to a file? Can you elaborate this a little more?

How I am going to stick numbers in front of them, or even get them out of the file seperately I am not sure.


You want to display some numbers (1,2,3....) while displaying the records or...?

...ask that no1 give...


Please UseRealWords when posting, it make confuses among the readers...



Hey, thanks for picking up on my mistakes. I have gone through and edited some things. Will try and do the same tomorrow when I have a better grasp of this problem. I hope it is a little more clearer now.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erik, I've modified your code a bit to prevent the forum from adding horizontal scroll bars. You had two lines of comment with a lot of unnecessary whitespace; they are now aligned to the matching statements. I've also split your last comment over two lines.
 
Erik Gruber
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Erik, I've modified your code a bit to prevent the forum from adding horizontal scroll bars. You had two lines of comment with a lot of unnecessary whitespace; they are now aligned to the matching statements. I've also split your last comment over two lines.



Thanks, I was trying to add the comments after I pasted the code and it wasn't letting me tab over so that's probably what happened.

Also, I am thinking of using a for-loop to iterate through the file course_list.txt(one loop for the headings and a second loop for the courses; or maybe one for each course?). I would rather find a better way that uses less code but at this point I'm not sure how else to go about it.

Any suggestions are welcome.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erik Gruber wrote:...I am thinking of using a for-loop to iterate through the file course_list.txt(one loop for the headings and a second loop for the courses; or maybe one for each course?). ...


Do you really need headings for the file? If you know the format you may not need it, and if you don't you may need to verify the content before process the data. Are the number of slots for a given course is fixed? Or you just read all the fields after the course id column (assuming all are time slots)?
 
Erik Gruber
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:

Erik Gruber wrote:...I am thinking of using a for-loop to iterate through the file course_list.txt(one loop for the headings and a second loop for the courses; or maybe one for each course?). ...



Do you really need headings for the file? If you know the format you may not need it, and if you don't you may need to verify the content before process the data. Are the number of slots for a given course is fixed? Or you just read all the fields after the course id column (assuming all are time slots)?



No I guess I really do not need the headings. I think you are right about verifying the content because I am not sure if my instructor will use a different file than the one given to me. In which case my for loop idea wouldn't work. I have to find a way for java to read through the file and pick out each course listing seperately. I am unsure how to go about doing this. I will update you guys if I make a breakthrough.
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic