-
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
COSC10972:Network Fundamentals
etc......
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.
Az
-
Az,
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.
Code:
Scanner input = new Scanner(new File("c:/java/home/src/forums/VehicleOne.txt"));
Cheers, Keith.
-
I have written a sample code for u.U make it as a structerd one.
import java.util.Scanner;
import java.io.File;
public class Test
{
public static void main(String args[]) throws Exception
{
/*My text file contains
COSC10974:Introduction to Programing
COSC10972:Network Fundamentals.
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[2];
Scanner sc = new Scanner(new File("test.txt"));
int i=0;
while(sc.hasNextLine())
{
//Delimit this line with scanner
Scanner sc1 = new Scanner(sc.nextLine());
sc1.useDelimiter(":");
courseObjArray[i] = new Course(sc1.next(),sc1.next());
i++;
}//while
for(int i1 = 0;i1 < courseObjArray.length;i1++)
{
System.out.println(courseObjArray[i1].courseCode);
System.out.println(courseObjArray[i1].courseName);
System.out.println("-----------------");
}
}//main
}//Test
class Course
{
String courseCode = null;
String courseName = null;
Course()
{
}
Course(String courseCode,String courseName)
{
this.courseCode = courseCode;
this.courseName = courseName;
}
}//Course
-