aspose file tools*
The moose likes Beginning Java and the fly likes Print strings from a file and select from a menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Print strings from a file and select from a menu" Watch "Print strings from a file and select from a menu" New topic
Author

Print strings from a file and select from a menu

Erik Gruber
Greenhorn

Joined: Oct 01, 2010
Posts: 6
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
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Erik Gruber
Greenhorn

Joined: Oct 01, 2010
Posts: 6
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Erik Gruber
Greenhorn

Joined: Oct 01, 2010
Posts: 6
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

Joined: Mar 24, 2008
Posts: 3775

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

Joined: Oct 01, 2010
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Print strings from a file and select from a menu
 
Similar Threads
Boolean problem
JRadiobutton problem
Determining the location of the file
Creating array of objects from user input and saving to a file
cannot find symbol inside String?