I am trying to design a file reader with some sort of GUI so I decided to use JOptionPane. I am a complete noob trying to learn java.

So far, I have managed to make my program produce a pop up for the file location, and also an error message if the program can't locate the file.
However, I cant get the results to display in one single box, it displays one for each line as I could only get it to work in the loop which would obviously do that. How do I go about displaying all the lines from the file in one output box?

package readtextfile;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        String file_name = JOptionPane.showInputDialog(null, "What's the File Location?");
                
        
        //"C:/Users/MyBook/Documents/TestProgram/testdocument.txt"

        try
        {
            ReadFile file = new ReadFile(file_name);
            String[] arrayLines = file.OpenContents();

            int i;
            for (i=0; i<arrayLines.length; i++)
            {
                JOptionPane.showMessageDialog(null, arrayLines[i]);
            }
        }

        //if there is an error, catch will produce error message
        catch (IOException errorMessage) //errorMessage is a variable, IOException type
        {
            JOptionPane.showMessageDialog(null, "File Not Found!");
            System.out.println(errorMessage.getMessage());
            System.out.printf("File %s can not be found.\n\n", file_name ); //getMessage is a method of IOException
        }
    }
}
package readtextfile;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile
{
    private String path;

    public ReadFile(String file_path)
    {
        path = file_path;
    }


    int readLines() throws IOException
    {
        FileReader file = new FileReader(path);
        BufferedReader buffer = new BufferedReader(file);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = buffer.readLine() ) !=null) //read each line until null value reached
        {
            numberOfLines++;
        }

        buffer.close();
        return numberOfLines;
    }

    public String[] OpenContents() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader buffer = new BufferedReader(fr);

        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

        for (i=0; i<numberOfLines; i++)
        {
            textData[i] = buffer.readLine();
        }

        buffer.close();
        return textData;
    }
}

How do I go about displaying all the lines from the file in one output box?

Concatenate the lines together separated by new line characters (\n)

i know how to do it like this:

String message = String.format("What's the File Location?\neg: C:/User/Desktop/Test.txt");
        String file_name = JOptionPane.showInputDialog(null, message);

but not when its an array? can you give me any more advice?

Remove the lines from the array.

thats what Im stuck on.. i tried to do that but the best I could do was get the last line of text from the array as the loop kept writting over the previous entry.

I am a complete newbie at this as I have only just started learning this. Could you possibly show me how its done? :)

Write a loop that goes thru the array and gets its elements one at a time and concatenates that element to the String to be shown in the JOptionPane.
After the loop ends, show the String.

how do i do it so that it doesn't overwrite the previous entry in the string? I tried that and just keeps overwritting the previous entry and showing me the last line in the txt file.

Post the code that shows what you are doing.

Do you know how to concatenate Strings?
aString += "add this to the String";

thanks that did the trick! the book I am using hasn't mentioned how to conatenate Strings yet so didnt know to do that, thanks for your help! :)

heres what I ended up doing:

try
        {
            ReadFile file = new ReadFile(file_name);
            String[] arrayLines = file.OpenContents();
            String displayMessage = null;
            

            int i;
            for (i=0; i<arrayLines.length; i++)
            {
                displayMessage += arrayLines[i];
                displayMessage += "\n";
            }
            JOptionPane.showMessageDialog(null, displayMessage);

Glad you got it to work.

Be a part of the DaniWeb community

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