Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Angry 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

  2. #2
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    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.

    Java Code:
    Scanner input = new Scanner(new File("c:/java/home/src/forums/VehicleOne.txt"));
    Cheers, Keith.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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

  4. #4
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

Similar Threads

  1. read from text file
    By rayda in forum New To Java
    Replies: 5
    Last Post: 04-10-2009, 03:51 AM
  2. Read and modify text file
    By heartysnowy in forum New To Java
    Replies: 27
    Last Post: 11-19-2008, 10:03 AM
  3. [SOLVED] How do I read from a text file
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:47 AM
  4. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •