Jump to content

BufferedReader

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
// The "DoubleArray" class.

import java.awt.*;

import hsa.*;

import java.io.*;


public class DoubleArray

{

    static Console c;           // The output console

    static final int total = 20;

    String[] numStr = new String [total];

    static double numDbl;

    static String stop = "";


    public void getAndStoreInput ()

    {

        int count = 0;

        do

        {

            c.print ("Enter number" + (count + 1));

            c.print (": ");


            while (true)

            {

                try

                {

                    numStr [count] = c.readLine ();

                    numDbl = Double.parseDouble (numStr [count]);

                    count++;

                    break;


                }


                catch (NumberFormatException e)

                {


                    new Message ("Invalid. Please Try Again");

                }


            }


        }

        while (count < total);


        try

        {

            File numArrays = new File ("NumberArrays.txt");

            FileWriter numWriter = new FileWriter (numArrays);

            BufferedWriter buffNumWriter = new BufferedWriter (numWriter);


            for (int i = 0 ; i < numStr.length ; i++)

            {

                buffNumWriter.write (numStr [i]);

                buffNumWriter.newLine ();

            }

            buffNumWriter.close ();

        }

        catch (IOException e)

        {

            new Message ("Error");

        }

    }



    public void displayInput ()

    {

        int i = 0;

        try

        {

            FileReader numReader = new FileReader ("NumberArrays.txt");

            BufferedReader buffNumReader = new BufferedReader (numReader);


            do

            {

                numStr [i] = buffNumReader.readLine ();

                i++;

            }

            while (numStr[i] != null);


            for (int u = numStr [i].length () - 1 ; u < -1 ; u--)

            {

                c.println (numStr [i]);

            }


        }

        catch (IOException e)

        {

            new Message ("Error");

        }


    }




    public static void main (String[] args)

    {

        c = new Console ();

        DoubleArray d = new DoubleArray ();

        d.getAndStoreInput ();

        d.displayInput ();



    } // main method

} // DoubleArray class




So that's my code. What I basically want to do is get 20 input from a user and save the 20 numbers line by line like:
12
13
14

Then read it and print it out in reverse order. But my displayInput method gets an error on the BufferedReader.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Please don't just say "there's an error". Be more specific and say which error and where. Or post the stacktrace.

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
It's hard to say because I can't run it. What is hsa?

Basically you're looking for this code though:

BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
        String[] input = new String[20];
        int i = 0;
        
        for (i = 0; i < 20; i++) {
            input[i] = fin.readLine();
            fin.read();
        }
        
        for (int x = 19; x>= 0; x--) {
            System.out.println(input[x]);
        }

Why are you using Console in some places for input and BufferedReader in other places? What is Console anyways?

But yeah, we need the stack trace or what exception is being thrown and where to better help figure out what is happening. Does the file exist? Is it throwing IOException?

Oh second look the error is probably here:


 do             {                 numStr [i] = buffNumReader.readLine ();                 i++;             }             while (numStr[i] != null);

When i is at 19 the program reads a line from the file into numStr[i] and makes i = 20 then tests if numStr[20] is NULL but this is out of bounds
in the array. Is that the error? If not, that will be an error you will encounter. You want the check in the while to be

while (numStr[i-1] != NULL)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users