Results 1 to 7 of 7
  1. #1
    littlefire is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default Reading input file into an array

    Hi

    Can you read the text from an input file into an array in an abstract class constructor or can you only read it in in a main class.

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    What happens when you try?

  3. #3
    littlefire is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    its not working. if we create an abstract constructor it looses the values after one iteration so i created it in main class but i can't pass it through to abstract class. i thought it might be to do with the string args in the main class?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    its not working
    You'lll have to describe it a bit more than that.
    Posting the code would be useful for anyone wanting to help.

  5. #5
    littlefire is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    this is the main class

    package zeroopt.tester;

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;

    import zeroopt.functions.*;
    import zeroopt.io.InputFile;
    import zeroopt.io.OutputFile;


    public class ZeroOptTest
    {
    public static void main( String[] args )
    {
    String inputFileName;
    String outputFileName;

    if ( args.length == 0 )
    {
    inputFileName = "C:\\Numerical_Analytics\\deirdre.txt";
    outputFileName = "swnum_1.out";
    }
    else if ( args.length == 1 )
    {
    inputFileName = args[0];
    outputFileName = "swnum_1.out";
    }
    else
    {
    inputFileName = args[0];
    outputFileName = args[1];
    }


    double xTolerence;
    String inputs[] = new String[4];

    String inputfilename = args[0];

    System.out.println("our filename is:"+ inputfilename);

    //FileReader fr;
    // BufferedReader br;
    int counter = 0;
    int maxIts;
    int i = 0;
    double xTolerance;
    String filename = "C:\\Numerical_Analytics\\deirdre.txt";
    FileReader fr = null;
    BufferedReader br;
    {
    // String filename = "C:\\Numerical_Analytics\\deirdre.txt";

    try {
    fr = new FileReader (filename);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    br = new BufferedReader( fr );
    String line;
    try {
    while ( (line = br.readLine()) != null )
    {
    StringTokenizer st = new StringTokenizer( line );
    while ( st.hasMoreTokens() )
    {
    //maxits = ( st.countTokens() );
    //maxits = Integer.parseInt(st.hasMoreTokens()) ;
    //inputs [i] = Integer.parseInt(st.nextToken()) ;
    inputs [i] = st.nextToken() ;
    System.out.println( "deirdre inputs"+ inputs[i] );
    i++;
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    maxIts = Integer.parseInt(inputs[0]);

    xTolerance = Double.valueOf(inputs[1].trim()).doubleValue();


    try
    {
    xTolerance = Double.valueOf(inputs[1].trim()).doubleValue();
    //System.out.println( "xTolerance "+ xTolerance );
    }
    catch (NumberFormatException nfe)
    {
    System.out.println( "NumberFormatException: "
    +nfe.getMessage());
    }


    System.out.println( "maxIts deirdre "+ maxIts );
    System.out.println( "xTolerance deirdre"+ xTolerance );



    InputFile inFile = new InputFile( inputFileName );
    OutputFile outFile = new OutputFile( outputFileName );



    ZeroOpt_f g = new ZeroOpt_f( inFile, outFile);
    ZeroOpt_f0 h = new ZeroOpt_f0( inFile, outFile);

    System.out.println( "Bisect f is " + g.zeroBisect() );
    System.out.println( "Bisect f0 is " + h.zeroBisect() );


    System.out.println( "Secant f is " + g.zero2Secant() );
    System.out.println( "Secant f0 is " + h.zero2Secant() );
    outFile.closeWriter( );
    inFile.closeReader( );

    } // end of method main()
    }
    }


    THIS IS ONE OF THE ABSTRACT METHODS IN OUR ABSTRACT CLASSES
    public
    double zeroBisect()

    {

    double x = 0;

    System.
    out.println("maxits is " + maxIts);

    for (int i = 1; i < maxIts; i++)

    {

    x=
    a+(b-a)/2.0;

    if (f(x) ==0.0)

    {

    return x;

    }

    else

    {

    if ((f(a)<0.0 && f(x) > 0.0 )|| (f(a)> 0.0 && f(x) < 0.0))

    {

    b = x;

    }

    else

    {

    a = x;

    x =
    a+(b-a)/2.0;

    }

    if (Math.abs(a-b) < xTolerance){

    return x;

    }

    }

    }

    return x;

    }
    /*end method zeroBisect*/

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Just another cross poster
    New To Java - reading input file into an array

    db

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM
  3. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 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
  •