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:
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
- 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.
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
}
- 05-04-2009, 07:39 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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
Similar Threads
-
Help! - How to insert a new line to a text file
By matpj in forum New To JavaReplies: 13Last Post: 02-24-2010, 05:28 PM -
writing to specific line in text file
By mickmos in forum New To JavaReplies: 2Last Post: 04-18-2009, 01:01 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 10Last Post: 10-24-2008, 07:21 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks