It was necessary to create a program that determines whether the test is a variable identifier. However, it must support Unicode encoding (identifiers may contain Cyrillic). In the first code, the program works properly, while in the second, you need to implement reading and writing data to a file. This is where the problem is, the line "shellaidғҲҮ124feast1111" in the first code allows the use as an identifier, but not in the second. The problem, I think, is that when it is read, it translates to another encoding, although UTF-8 is indicated. How to fix, I do not know, I ask for your help, thanks in advance.
First code
package la6; import java.io.UnsupportedEncodingException; import java.util.Scanner; import java.util.Arrays; public class La6 { public static void main(String[] args) throws UnsupportedEncodingException { Scanner in = new Scanner(System.in); System.out.println("Enter ID's name: "); String str = in.nextLine(); if (isValidIdentifier(str)) { System.out.println("«" + str + "» - допустимое значение"); } else { System.out.println("«" + str + "» - недопустимое значение"); } System.out.println("У™іТЈТ“ТЇТ±Т›У©Т»"); System.out.println(); } public static boolean isValidIdentifier(String str) { if (!str.matches("[$_\u0401-\u04F9\u0041-\uFB06]+[$_0-9\u0401-\u04F9\u0041-\uFB06]+")) { return false; } String[] keyWords = {"boolean","volatile","else","instanceof","switch","true","goto","abstract","enum","int","static","false","break","assert","extends","interface","strictfp","null","continue","byte","final","long","super","do","case","finally","native","synchronized","double","catch","float","new","throw","protected","char","for","package","throws","this","class","if","private","transient","short","const","implements","public","try","while","default","import","return","void", "/", "","""}; if (Arrays.asList(keyWords).contains(str)) { return false; } return true; } } Second code:
package tk.lab7; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; public class TKLab7 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); System.out.print("Введите имя файла для чтения: "); String name = in.readLine(); /// Выбор кодировки чтения файла //System.out.println("\nУкажите кодировку: \n1: ANSI\n2: Юникод\nлюбая другая клавиша: UTF-8"); String /*code = in.readLine(); if ("1".equals(code)) */code = "UTF-8"; /*else if ("2".equals(code)) code = "UTF-16"; else code = "UTF-8";*/ //считывание из файла BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".txt"), code)); String original_text = ""; int c; while((c=reader.read())!=-1){ original_text += ((char)c); } //вывод текста из файла в консоль System.out.println("\nПроверяемая строка: "); System.out.print(original_text); System.out.print("\nВведите имя файла для записи: "); String name2 = in.readLine(); // запись измененного теста в файл OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(name2 + ".txt", false), code); if (isValidIdentifier(original_text)) { writer.write("«" + original_text + "» - допустимое значение"); } else { writer.write("«" + original_text + "» - недопустимое значение"); } System.out.println(); //очистка буфера writer.flush(); } public static boolean isValidIdentifier(String original_text) { if (!original_text.matches("[$_\u0401-\u04F9\u0041-\uFB06]+[$_0-9\u0401- \u04F9\u0041-\uFB06]+")) { return false; } String[] keyWords = {"boolean","volatile","else","instanceof","switch","true","goto","abstract","enum","int","static","false","break","assert","extends","interface","strictfp","null","continue","byte","final","long","super","do","case","finally","native","synchronized","double","catch","float","new","throw","protected","char","for","package","throws","this","class","if","private","transient","short","const","implements","public","try","while","default","import","return","void", "/", "","""}; if (Arrays.asList(keyWords).contains(original_text)) { return false; } return true; } }