Results 1 to 4 of 4
Thread: Read from a text file?
- 04-29-2009, 12:45 PM #1
Read from a text file?
I have written a class called "Course", this class has instance variables of "courseCode" and "courseName". I want to create multiple Course objects, but use a text file to input the data into the objects.
The text file is in the format:
COSC10974:Introduction to Programing
The course code is the first part "COSC10974" and the course name is the second part "Introduction to Programing". The text file delimiters each field by a colon(:).
I have saved the text file "courses.txt" in the same directory as my class files for this project.
I want to use an array of 16 Course objects, which is to loaded from the "courses.txt" file.
If someone could shed some light on how I read from an existing file into an array of objects that would be great.
Also, where do you save text files that are read by the program? My atemps so far: because the text file is in the same directory as the class I have created. I have refered to it as:
java.io.File file = new java.io.File("courses.txt");
Scanner input = new Scanner(new File("courses.txt"));
I'm pretty lost.
Any help would be appreciated.
- 04-29-2009, 02:53 PM #2Member
- Join Date
- Apr 2009
- Rep Power
When I saw your question, the first I thing I did was double-press the right alt-key to bring up google-desktop's search-box. I typed Scanner SE 6 into the search-box and pressed enter. The first hit is Scanner (Java Platform SE 6) ... So I click on it and scroll down and find the useDelimiter method Scanner (Java Platform SE 6)) (I knew it had one, but couldn't remember what it was called, or the details of it's usage.
Hmmm... looks promising... ey what?
Now what was your specific question again? Where to put a textfile in order to read it from a Java app... Well that (unfortunately) depends on how you're running the program.
Are you using an IDE? Which one? What O/S?
If you are using an IDE then I recommend that you revert to the good ole' command line tools to compile and run your Java programs until such time as you have mastered the Java classpath; and can build, run, package, and deploy a Java application succesfuly.
If you are using the command line... the file simply needs to be in the current working directory when you execute the program... and any relative file-paths within the pogram will be relative to that directory (until such-time as you change directories, but let's no go there, it's a bit complicated.
The other (probably more reliable, but much less portable) option is to supply the canonical-path (i.e. the complete path) to the file.
Scanner input = new Scanner(new File("c:/java/home/src/forums/VehicleOne.txt"));
- 04-30-2009, 11:12 AM #3
I have written a sample code for u.U make it as a structerd one.
public class Test
public static void main(String args) throws Exception
/*My text file contains
COSC10974:Introduction to Programing
So my Array length is just 2.
Using Scanner iam reading.then delimiting each string with ":" and storing it into Course object
Course courseObjArray = new Course;
Scanner sc = new Scanner(new File("test.txt"));
//Delimit this line with scanner
Scanner sc1 = new Scanner(sc.nextLine());
courseObjArray[i] = new Course(sc1.next(),sc1.next());
for(int i1 = 0;i1 < courseObjArray.length;i1++)
String courseCode = null;
String courseName = null;
Course(String courseCode,String courseName)
this.courseCode = courseCode;
this.courseName = courseName;
- 05-01-2009, 04:42 AM #4
- By rayda in forum New To JavaReplies: 5Last Post: 04-10-2009, 03:51 AM
- By heartysnowy in forum New To JavaReplies: 27Last Post: 11-19-2008, 11:03 AM
- By matzahboy in forum New To JavaReplies: 5Last Post: 11-17-2008, 05:47 AM
- By janeansley in forum New To JavaReplies: 40Last Post: 07-04-2008, 08:41 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 10:13 AM