创建一个类,将文本文件的内容以一些变更显示在控制台上。

huangapple 未分类评论45阅读模式
英文:

Create a class that displays the content of the text file to console with some changes

问题

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Part1 {
  7. // 获取文件中的输入字符串
  8. public static String getInput(String fileName) {
  9. StringBuilder sb = new StringBuilder();
  10. try {
  11. Scanner scanner = new Scanner(new File(fileName), "UTF-8");
  12. while (scanner.hasNextLine()) {
  13. sb.append(scanner.nextLine()).append(System.lineSeparator());
  14. }
  15. scanner.close();
  16. return sb.toString().trim();
  17. } catch (IOException ex) {
  18. ex.printStackTrace();
  19. }
  20. return sb.toString();
  21. }
  22. // 删除长度为4及以上的单词的前两个字符
  23. public static void convert() {
  24. try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
  25. String line;
  26. StringBuilder result = new StringBuilder();
  27. while ((line = in.readLine()) != null) {
  28. String[] wordsInLine = line.split("[.,!?\\-\\s\\n]+");
  29. for (String string : wordsInLine) {
  30. if (isLongerThanFour(string) && defineLocale(string).equals("latn")) {
  31. result.append(string.substring(2)).append(" ");
  32. } else if (isLongerThanFour(string) && defineLocale(string).equals("cyrl")) {
  33. result.append(string.substring(4)).append(" ");
  34. } else {
  35. result.append(string).append(" ");
  36. }
  37. }
  38. }
  39. System.out.println(result);
  40. } catch (IOException e) {
  41. e.getMessage();
  42. }
  43. }
  44. // 检查当前单词的长度是否大于等于4
  45. public static boolean isLongerThanFour(String string) {
  46. return string.length() >= 4;
  47. }
  48. // 判断输入单词的语言(西里尔字母或拉丁字母)
  49. private static String defineLocale(String string) {
  50. char ch = string.charAt(0);
  51. if (Character.isAlphabetic(ch)) {
  52. if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
  53. return "cyrl";
  54. } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)) {
  55. return "latn";
  56. }
  57. }
  58. return "none";
  59. }
  60. public static void main(String[] args) {
  61. Part1.convert();
  62. }
  63. }

以上是你提供的代码的中文翻译,保留了代码的结构和原意,同时进行了一些格式上的调整。如果你有任何进一步的问题或需要更多帮助,请随时提问。

英文:

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.

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Part1 {
  7. // getting input string from the file
  8. public static String getInput(String fileName) {
  9. StringBuilder sb = new StringBuilder();
  10. try {
  11. Scanner scanner = new Scanner(new File(fileName), "UTF-8");
  12. while (scanner.hasNextLine()) {
  13. sb.append(scanner.nextLine()).append(System.lineSeparator());
  14. }
  15. scanner.close();
  16. return sb.toString().trim();
  17. } catch (IOException ex) {
  18. ex.printStackTrace();
  19. }
  20. return sb.toString();
  21. }
  22. // deleting the first two characters of each word with the length of 4 and more characters
  23. public static void convert() {
  24. try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
  25. String line;
  26. StringBuilder result = new StringBuilder();
  27. while ((line = in.readLine()) != null) {
  28. String[] wordsInLine = line.split("[.,!?\\-\\s\\n]+");
  29. for (String string : wordsInLine) {
  30. if (isLongerThanFour(string) && defineLocale(string) == "latn") {
  31. result.append(string.substring(2) + " ");
  32. } else if (isLongerThanFour(string) && defineLocale(string) == "cyrl") {
  33. result.append(string.substring(4) + " ");
  34. } else {
  35. result.append(string + " ");
  36. }
  37. }
  38. }
  39. System.out.println(result);
  40. }catch(IOException e){
  41. e.getMessage();
  42. }
  43. }
  44. // checking for right length of current word
  45. public static boolean isLongerThanFour(String string){
  46. return string.length() >= 4;
  47. }
  48. // define language of input word(one of cyrillic of latin languages)
  49. private static String defineLocale(String string) {
  50. char ch = string.charAt(0);
  51. if (Character.isAlphabetic(ch)) {
  52. if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
  53. return "cyrl";
  54. } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
  55. return "latn";
  56. }
  57. }
  58. return "none";
  59. }
  60. public static void main(String[] args){
  61. Part1.convert();
  62. }
  63. }

Can you point on my mistakes or suggest cleaner solution.

Thank you in advance.

答案1

得分: 0

  1. public class Converter {
  2. // 辅助枚举
  3. enum Language {
  4. cyr,
  5. lat,
  6. none
  7. }
  8. // 如果需要返回超过两种类型的值,请使用枚举
  9. private static Language defineLocale(String string) {
  10. char ch = string.charAt(0);
  11. if (Character.isAlphabetic(ch)) {
  12. if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
  13. return Language.cyr;
  14. } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
  15. return Language.lat;
  16. }
  17. }
  18. return Language.none;
  19. }
  20. public void convert() {
  21. try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
  22. String line;
  23. StringBuilder result = new StringBuilder();
  24. while ((line = in.readLine()) != null) {
  25. String[] wordsInLine = line.split(" ");
  26. for (String s : wordsInLine) {
  27. if (s.length() > 3) {
  28. switch (defineLocale(s)) {
  29. case cyr:
  30. result.append(s.substring(4));
  31. break;
  32. case lat:
  33. result.append(s.substring(2));
  34. break;
  35. default:
  36. result.append(s);
  37. }
  38. } else result.append(s);
  39. result.append(" ");
  40. }
  41. result.append("\n");//你遗漏的部分
  42. }
  43. System.out.println(result);
  44. }catch(IOException e){
  45. e.getMessage();
  46. }
  47. }
  48. public static void main(String[] args){
  49. new Converter().convert();
  50. }
  51. }
英文:
  1. public class Converter {
  2. //helper enum
  3. enum Language {
  4. cyr,
  5. lat,
  6. none
  7. }
  8. // if you have to return more that two types of values then use enum
  9. private static Language defineLocale(String string) {
  10. char ch = string.charAt(0);
  11. if (Character.isAlphabetic(ch)) {
  12. if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
  13. return Language.cyr;
  14. } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
  15. return Language.lat;
  16. }
  17. }
  18. return Language.none;
  19. }
  20. public void convert() {
  21. try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {
  22. String line;
  23. StringBuilder result = new StringBuilder();
  24. while ((line = in.readLine()) != null) {
  25. String[] wordsInLine = line.split(" ");
  26. for (String s : wordsInLine) {
  27. if (s.length() > 3) {
  28. switch (defineLocale(s)) {
  29. case cyr:
  30. result.append(s.substring(4));
  31. break;
  32. case lat:
  33. result.append(s.substring(2));
  34. break;
  35. default:
  36. result.append(s);
  37. }
  38. } else result.append(s);
  39. result.append(" ");
  40. }
  41. result.append("\n");//all you were missing
  42. }
  43. System.out.println(result);
  44. }catch(IOException e){
  45. e.getMessage();
  46. }
  47. }
  48. public static void main(String[] args){
  49. new Converter().convert();
  50. }
  51. }

I hope this does not need any further explanation but dont be shy to ask if you dont understand something.

huangapple
  • 本文由 发表于 2020年7月26日 04:11:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/63093118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定