英文:
Create a class that displays the content of the text file to console with some changes
问题
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Part1 {
// 获取文件中的输入字符串
public static String getInput(String fileName) {
StringBuilder sb = new StringBuilder();
try {
Scanner scanner = new Scanner(new File(fileName), "UTF-8");
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine()).append(System.lineSeparator());
}
scanner.close();
return sb.toString().trim();
} catch (IOException ex) {
ex.printStackTrace();
}
return sb.toString();
}
// 删除长度为4及以上的单词的前两个字符
public static void convert() {
try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
String line;
StringBuilder result = new StringBuilder();
while ((line = in.readLine()) != null) {
String[] wordsInLine = line.split("[.,!?\\-\\s\\n]+");
for (String string : wordsInLine) {
if (isLongerThanFour(string) && defineLocale(string).equals("latn")) {
result.append(string.substring(2)).append(" ");
} else if (isLongerThanFour(string) && defineLocale(string).equals("cyrl")) {
result.append(string.substring(4)).append(" ");
} else {
result.append(string).append(" ");
}
}
}
System.out.println(result);
} catch (IOException e) {
e.getMessage();
}
}
// 检查当前单词的长度是否大于等于4
public static boolean isLongerThanFour(String string) {
return string.length() >= 4;
}
// 判断输入单词的语言(西里尔字母或拉丁字母)
private static String defineLocale(String string) {
char ch = string.charAt(0);
if (Character.isAlphabetic(ch)) {
if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
return "cyrl";
} else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)) {
return "latn";
}
}
return "none";
}
public static void main(String[] args) {
Part1.convert();
}
}
以上是你提供的代码的中文翻译,保留了代码的结构和原意,同时进行了一些格式上的调整。如果你有任何进一步的问题或需要更多帮助,请随时提问。
英文:
Changes: delete the first two characters of each word with the length of 4 and more characters
example: original 'qwerty', new 'erty'
A 'word' should be considered a continuous sequence of Cyrillic or Latin characters.
I wrote something like this, but output string is in line, but I need input text with indent characters.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Part1 {
// getting input string from the file
public static String getInput(String fileName) {
StringBuilder sb = new StringBuilder();
try {
Scanner scanner = new Scanner(new File(fileName), "UTF-8");
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine()).append(System.lineSeparator());
}
scanner.close();
return sb.toString().trim();
} catch (IOException ex) {
ex.printStackTrace();
}
return sb.toString();
}
// deleting the first two characters of each word with the length of 4 and more characters
public static void convert() {
try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
String line;
StringBuilder result = new StringBuilder();
while ((line = in.readLine()) != null) {
String[] wordsInLine = line.split("[.,!?\\-\\s\\n]+");
for (String string : wordsInLine) {
if (isLongerThanFour(string) && defineLocale(string) == "latn") {
result.append(string.substring(2) + " ");
} else if (isLongerThanFour(string) && defineLocale(string) == "cyrl") {
result.append(string.substring(4) + " ");
} else {
result.append(string + " ");
}
}
}
System.out.println(result);
}catch(IOException e){
e.getMessage();
}
}
// checking for right length of current word
public static boolean isLongerThanFour(String string){
return string.length() >= 4;
}
// define language of input word(one of cyrillic of latin languages)
private static String defineLocale(String string) {
char ch = string.charAt(0);
if (Character.isAlphabetic(ch)) {
if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
return "cyrl";
} else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
return "latn";
}
}
return "none";
}
public static void main(String[] args){
Part1.convert();
}
}
Can you point on my mistakes or suggest cleaner solution.
Thank you in advance.
答案1
得分: 0
public class Converter {
// 辅助枚举
enum Language {
cyr,
lat,
none
}
// 如果需要返回超过两种类型的值,请使用枚举
private static Language defineLocale(String string) {
char ch = string.charAt(0);
if (Character.isAlphabetic(ch)) {
if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
return Language.cyr;
} else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
return Language.lat;
}
}
return Language.none;
}
public void convert() {
try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
String line;
StringBuilder result = new StringBuilder();
while ((line = in.readLine()) != null) {
String[] wordsInLine = line.split(" ");
for (String s : wordsInLine) {
if (s.length() > 3) {
switch (defineLocale(s)) {
case cyr:
result.append(s.substring(4));
break;
case lat:
result.append(s.substring(2));
break;
default:
result.append(s);
}
} else result.append(s);
result.append(" ");
}
result.append("\n");//你遗漏的部分
}
System.out.println(result);
}catch(IOException e){
e.getMessage();
}
}
public static void main(String[] args){
new Converter().convert();
}
}
英文:
public class Converter {
//helper enum
enum Language {
cyr,
lat,
none
}
// if you have to return more that two types of values then use enum
private static Language defineLocale(String string) {
char ch = string.charAt(0);
if (Character.isAlphabetic(ch)) {
if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
return Language.cyr;
} else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
return Language.lat;
}
}
return Language.none;
}
public void convert() {
try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
String line;
StringBuilder result = new StringBuilder();
while ((line = in.readLine()) != null) {
String[] wordsInLine = line.split(" ");
for (String s : wordsInLine) {
if (s.length() > 3) {
switch (defineLocale(s)) {
case cyr:
result.append(s.substring(4));
break;
case lat:
result.append(s.substring(2));
break;
default:
result.append(s);
}
} else result.append(s);
result.append(" ");
}
result.append("\n");//all you were missing
}
System.out.println(result);
}catch(IOException e){
e.getMessage();
}
}
public static void main(String[] args){
new Converter().convert();
}
}
I hope this does not need any further explanation but dont be shy to ask if you dont understand something.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论