Question

How to get String from JTextField or JTextArea with Polish letters? when I use myTxtArea.getText() and in my JTextArea I've got ĄĆĘŁŃÓŚŹŻąćęłńóśźż it only recognizes ?????Ó????????ó???.

Text from both JTextField and JTextArea is going to be saved in *.txt file

That's my code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyAppCreateAcc {

public static void main(String[] args) throws IOException {
        AppGUI ag = new AppGUI();
    }
public MyAppCreateAcc() throws IOException {

    String group = AppGUI.getGroup().getText();

    String[] lines = AppGUI.getNote().getText().split("\\n");

    BufferedWriter w = new BufferedWriter(new FileWriter(
            "zaloz_konta.cmd"));
    w.write("mode con codepage select=1250");
    w.newLine();

    for (int j = 0; j < lines.length; j++) {

        /*
         * delete more than one space between words and from the full name
         * create a short one first letter of name and surname after all
         * lowercase like on example: John Smith = jsmith
         */
        if (lines[j] == null || lines[j].equals("")) {

            // if there is a gap between the names do nothing

        } else {
            lines[j] = lines[j].trim().replaceAll(" +", " ");
            Pattern pattern = Pattern.compile("\\s([A-Za-z]+)");
            Matcher matcher = pattern.matcher(lines[j]);
            String shortName = "";
            if (matcher.find()) {
                shortName = (lines[j].charAt(0) + matcher.group(1))
                        .toLowerCase();
            }

            w.write("call konto.cmd " + shortName + " \"" + lines[j]
                    + "\" 123 " + "\"" + group + "\"");
            w.newLine();
        }
    }
    w.close();
}

}

I want to get Polish letters just from:

String[] lines = AppGUI.getNote().getText().split("\\n");

Was it helpful?

Solution

for JTextCompoents to use

by default accepting separators and Files encode page

OTHER TIPS

You want to use UTF-8 Encoding. Give that a try

 String s = new String(AppGUI.getNote().getText().getBytes(), "UTF-8");
 String[] lines = s.split("\\n");     

I extended my pattern and it helped as well:

lines[j] = lines[j].trim().replaceAll(" +", " ");
Pattern pattern = Pattern.compile("\\s([A-Za-ząćęłńóśźżĄĘŁĆŃÓŚŹŻ]+)");
Matcher matcher = pattern.matcher(lines[j]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top