Article View: aus.computers.java
Article #135Writing Java driver to read from textfile
From: M.T.
Date: Mon, 22 Aug 2005 22:35
Date: Mon, 22 Aug 2005 22:35
72 lines
2518 bytes
2518 bytes
I have a class to read text input from a text file one line at a time.
I am having great difficulty writing a driver to text this class.
Thanks
Margaret
// Class TextIO
// required imports
import java.io.*;
class TextIO
{
public static void fileRead() throws IOException{
int total = 0;
int value;
String line;
BufferedReader inFile=null;
String fileName="data.txt";
try{
inFile=new BufferedReader(new FileReader (fileName));
while (true){
try{
line=inFile.readLine();
if (line==null)
break;
value=Integer.parseInt(line);
total+=value;
}
catch(NumberFormatException exception){
System.out.println("That's not a number");
}
}
System.out.println("Total is: " + total);
inFile.close();
} catch (FileNotFoundException e){
throw e;
}
}//end fileRead method
}
TextTest.java
// Class TextTest
// calls to test the methods in TextIO
// required imports
import java.io.*;
class TextTest
{
public static void main(String[] argv){
// declare variables
String fileRead;
System.out.println("Start Testing TextClass");
//************* - what am I getting from TextIO ????????
fileRead = TextIO.fileRead();
System.out.println("The string returned is: "+ fileRead);
System.out.println("Finish Testing TextClass");
}
}
Technical Details
Message-ID:
<_qsOe.7495$FA3.6160@news-server.bigpond.net.au>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news.exile.org!abnormal.com!lon-transit.news.telstra.net!lon-in.news.telstra.net!news.telstra.net!news-server.bigpond.net.au!53ab2750!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10d6db,9a2084d14b6f8514,start X-Google-Attributes: gid10d6db,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news.exile.org!abnormal.com!lon-transit.news.telstra.net!lon-in.news.telstra.net!news.telstra.net!news-server.bigpond.net.au!53ab2750!not-for-mail From: "M.T." <tuross92@hotmail.com> Newsgroups: aus.computers.java Subject: Writing Java driver to read from textfile Lines: 74 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2527 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 X-RFC2646: Format=Flowed; Original Message-ID: <_qsOe.7495$FA3.6160@news-server.bigpond.net.au> Date: Mon, 22 Aug 2005 22:35:38 GMT NNTP-Posting-Host: 203.51.92.224 X-Complaints-To: abuse@bigpond.net.au X-Trace: news-server.bigpond.net.au 1124750138 203.51.92.224 (Tue, 23 Aug 2005 08:35:38 EST) NNTP-Posting-Date: Tue, 23 Aug 2005 08:35:38 EST Organization: BigPond Internet Services