Problem with area.addText (char ...) JAVA

0

I try to read a file from java, and add it to a JTextArea:

    if (e.getSource() == go) {
        try {
            String strA = field1.getText();
            FileReader archr1 = new FileReader(strA);
            int valor = archr1.read();

            while(valor!=-1) {
                System.out.print((char)valor);
                area1.setText((char)valor);
                valor = archr1.read();
            } //fin while

            archr1.close();

        } catch(IOException r) {
            area1.setText("Error: "+r);
        } // fin catch
    }

To verify that you read the file, I use the print (char) value and if it shows me all the content.

But when I add it to the JTextArea (area1) it returns this error:

"error: incompatible types: char cannot be converted to String"

How could I convert the final result into a String, so that I can show the content in the textarea?

Thank you very much.

    
asked by Javier Avila Fernandez 21.05.2018 в 12:09
source

1 answer

1

try this:

if (e.getSource() == go) {
        try {
            String strA = field1.getText();
            FileReader archr1 = new FileReader(strA);
            int valor = archr1.read();
            String miString = "";
        while(valor!=-1) {
           System.out.print((char)valor);
           char miChar = (char)valor;
           miString += Character.toString(miChar);
            valor = archr1.read();
        } //fin while
        area1.setText(miString);
        archr1.close();

    } catch(IOException r) {
        area1.setText("Error: "+r);
    } // fin catch
}
    
answered by 21.05.2018 / 12:12
source