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

    Angry Reading part of a line in a text file

    Hi all, I want to read from a text file and pass each line of that file into an array of objects.

    Here's the text file format:

    12345678:Aaron:Simons:Y:COSC1070-55:COSC1071-56:COSC07298-89
    23456789:Toni:Simons:Y:COSC1070-23:COSC1071-34:COSC1072-67


    etc....

    I have created a class "Student" that has instance variables of:

    int studentNumber;
    String firstName;
    String lastName;
    String paidFees;
    Result[] results = new Result[3];


    The Result class is made up of "courseCode" and "result". So you can see that the text file has the first four values as primatives and the last three values as Result Objects.

    I've used a Scanner to read from similar text files to instantiate other objects, so that part is not my problem.

    What I need to do is firstly create and array just consisting of the results from the text file(deliniated by a "-") and then pass that array into the Student constructor.

    How do I only read the results values of the text file, that is, skip the 12345678:Aaron:Simons:Y and just read the COSC1070-55:COSC1071-56:COSC07298-89. Also, if each of the text file values id deliniated by a (":"), and within the result value, the two parts are deliniated by a ("-"), how do I just pick out the String "COSC1070" and the Integer "55" and pass those values into the result array?

    Hope someone can help, thanks

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

    Default

    This code will give u the courseCode and courseMark as whyat u asked.Please create the Resultclass and store the value.

    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.File;
    public class Test
    {
    public static void main (String args [])
    {
    Scanner sc = null;
    Scanner delimitScanner;
    Test obj = new Test();
    try
    {
    //Read the texFile
    sc = new Scanner(new File("test.txt"));
    String inputLine="";
    String finalOutputLine="";
    while(sc.hasNext())
    {
    inputLine = sc.nextLine();
    //After reading taking only courseCode and courseMark like this below
    //COSC1070-55:COSC1071-56:COSC07298-89
    //Passing the above String for delimiting with ":"
    finalOutputLine = inputLine.substring(inputLine.indexOf("COSC"),inpu tLine.length());
    obj.delimitTheString(finalOutputLine);

    }
    }catch(Exception ex)
    {
    System.out.println("Exception "+ ex);
    }
    }//main

    /* Below method will give courseCode and courseMark separatley.Please code the Result class and store this coursecode and
    coursemark **/
    public void delimitTheString(String finalOutputLine)
    {
    StringTokenizer st = new StringTokenizer(finalOutputLine,":");
    String finalToken="";
    String courseCode="";
    String courseMark="";

    while(st.hasMoreTokens())
    {
    finalToken = st.nextToken();
    courseCode = finalToken.substring(finalToken.indexOf("COSC"),fi nalToken.indexOf("-"));
    System.out.println("courseCode "+courseCode);
    courseMark = finalToken.substring(finalToken.indexOf("-")+1,finalToken.length());
    System.out.println("courseMark "+courseMark);
    }
    }//delimitTheString
    }

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    as you know the structure of the file
    you can read the whole line at once

    use String.split to split the line into pieces by ":"
    then create student object by first 4 element 12345678, Aaron, Simons and Y

    then 5th,6th,7th element used to Result object
    for each 5-7th element, use String.split again to split COSC1070-55 by "-"
    Last edited by mtyoung; 05-04-2009 at 08:12 AM.

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

Similar Threads

  1. Help! - How to insert a new line to a text file
    By matpj in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 05:28 PM
  2. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 01:01 PM
  3. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 07:21 PM
  4. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 12:31 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •