How do i read a txt file into a char array. what i have so far only give me blank lines

String fname = "c:\\game.txt";
    char data;
    int row = 2;
        int col = 4;
        char[] matrix = new char[row];

private void openFile()
    {

        try
        {
            FileReader reader = new FileReader(this.fname);

            for (int i=0; i<matrix.length; i++)
            {
                reader.read();
            }
            reader.close();


        }
        catch(Exception e)
        {
            String s = e.toString();
            System.out.println(s);
        }

    }

Recommended Answers

Of course it does - you aren't saving what is read anywhere:

reader.read();

If you want the data read into a char[] buffer then you have to supply it as an argument to the read() method. Check the API for

Jump to Post

So perhaps you should close this thread, since you have already started another on the exact same topic.

Jump to Post

All 5 Replies

Of course it does - you aren't saving what is read anywhere:

reader.read();

If you want the data read into a char[] buffer then you have to supply it as an argument to the read() method. Check the API for read(). There are a few variations available depending on your needs.

i eventually want to read the file into a 2d array. i want to be able to find a particular character in the matrix.

So perhaps you should close this thread, since you have already started another on the exact same topic.

i have a file.txt like this : 2X^2 + 5X+3=0
i want to save 2 to variable a, 5 to variable b, and 3 to variable c . .
how i can do that . . ?
tanks for your help . .

@gunvesxco Please start your own thread with your question. This thread is very old.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.