Results 1 to 4 of 4
- 05-04-2009, 05:58 AM #1
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:
I have created a class "Student" that has instance variables of:
Result results = new Result;
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
- 05-04-2009, 07:37 AM #2
This code will give u the courseCode and courseMark as whyat u asked.Please create the Resultclass and store the value.
public class Test
public static void main (String args )
Scanner sc = null;
Test obj = new Test();
//Read the texFile
sc = new Scanner(new File("test.txt"));
inputLine = sc.nextLine();
//After reading taking only courseCode and courseMark like this below
//Passing the above String for delimiting with ":"
finalOutputLine = inputLine.substring(inputLine.indexOf("COSC"),inpu tLine.length());
System.out.println("Exception "+ ex);
/* Below method will give courseCode and courseMark separatley.Please code the Result class and store this coursecode and
public void delimitTheString(String finalOutputLine)
StringTokenizer st = new StringTokenizer(finalOutputLine,":");
finalToken = st.nextToken();
courseCode = finalToken.substring(finalToken.indexOf("COSC"),fi nalToken.indexOf("-"));
courseMark = finalToken.substring(finalToken.indexOf("-")+1,finalToken.length());
- 05-04-2009, 07:39 AM #3Senior Member
- Join Date
- Dec 2008
- Hong Kong
- Rep Power
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.
- 05-04-2009, 07:57 AM #4
- By matpj in forum New To JavaReplies: 13Last Post: 02-24-2010, 05:28 PM
- By mickmos in forum New To JavaReplies: 2Last Post: 04-18-2009, 01:01 PM
- By jadaleus in forum Advanced JavaReplies: 10Last Post: 10-24-2008, 07:21 PM
- By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM
- By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM