🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.computers.java
Article #49

Re: Writing Java driver to read from textfile

#49
From: Ricka
Date: Wed, 12 Oct 2005 12:13
90 lines
3081 bytes
M.T. wrote:
> 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");
>  }
> } 
> 
> 

If you have a look at your declaration for the fileRead() method, you'll 
see that you've stated it returns NOTHING! (void). I'm not sure what it 
is that you want returned from the fileRead method. Do you want one 
entire string of the whole text, or do you want the integer value total?

try changing

 >  public static void fileRead() throws IOException{

to something like

   public static int fileRead() throws IOException{

and make it return the int value total
Technical Details
Message-ID: <434cfdd5$0$6771$afc38c87@news.optusnet.com.au>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news1.optus.net.au!optus!newsfeeder.syd.optusnet.com.au!news.optusnet.com.au!not-for-mail
References: <_qsOe.7495$FA3.6160@news-server.bigpond.net.au>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10d6db,9a2084d14b6f8514
X-Google-Attributes: gid10d6db,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news1.optus.net.au!optus!newsfeeder.syd.optusnet.com.au!news.optusnet.com.au!not-for-mail
Date: Wed, 12 Oct 2005 22:13:16 +1000
From: Ricka <pezza48@hotmail.com>
User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: aus.computers.java
Subject: Re: Writing Java driver to read  from textfile 
References: <_qsOe.7495$FA3.6160@news-server.bigpond.net.au>
In-Reply-To: <_qsOe.7495$FA3.6160@news-server.bigpond.net.au>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 90
Message-ID: <434cfdd5$0$6771$afc38c87@news.optusnet.com.au>
NNTP-Posting-Host: 220.238.133.234
X-Trace: 1129119190  6771 220.238.133.234