java - 有人可以检查我关于替换 .txt 文件中的数字的代码吗?我无法替换其中的数字

标签 java io pattern-matching matcher

我有以下代码,用于替换文本文件中的数字:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;
import java.util.Scanner;

public class L20 {

   public static void main(String[] args) throws IOException {
      Scanner input = new Scanner(System.in);

      System.out.println("Enter file name");
      String in = input.nextLine();
      try {
         textWriter(in);
         textReader(in);
         textChanger(in);
      } catch (Exception e) {
      }
   }

   public static void textWriter(String path) throws IOException {
      String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "m", "l", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
            "x", "y", "z" };
      File file = new File(path);
      Writer output = null;
      Random number = new Random();
      output = new BufferedWriter(new FileWriter(file));
      int lines = 10 + number.nextInt(11);
      for (int i = 0; i < lines; i++) {
         int it2 = 1 + number.nextInt(9);
         int n1 = number.nextInt(26);
         int n2 = number.nextInt(26);
         int n3 = number.nextInt(26);

         String t2 = Integer.toString(it2);
         String t1 = alphabet[n1] + alphabet[n2] + alphabet[n3];
         String text = t1 + t2;
         output.write(text);
         ((BufferedWriter) output).newLine();
      }
      output.close();
      System.out.println("Your file has been written");
   }

   public static void textReader(String path) throws IOException {
      File file = new File(path);
      Scanner input;
      input = new Scanner(file);
      String line;
      while ((line = input.nextLine()) != null) {
         System.out.println(line);
      }
      input.close();
   }
}

但不幸的是,代码无法正常工作。问题部分如下所示:

public static void textChanger(String path) throws IOException {
   File file = new File(path);

   Scanner input;
   input = new Scanner(file);
   String line;
   Pattern p = Pattern.compile("[0-9]");

   while ((line = input.nextLine()) != null) {
      Matcher m = p.matcher(line);
      int n = Integer.parseInt(m.group());
      System.out.println(n);
   }

   input.close();
}

我想要做的是仅替换 Text.txt 文件中每行的数字部分。每一行都由三个字母和一个数字组成。我只想更改数字部分。

最佳答案

试试这个............

public class Reg {

    public static void main(String[] args){

        String s = "abc1";
        Pattern p = Pattern.compile("\\d");
        Matcher m = p.matcher(s);
        String no = new String();

        while (m.find()){

              no = m.group();
        }

        String newStr = s.replace(no, "hi");
        System.out.println(newStr);
       }
}

关于java - 有人可以检查我关于替换 .txt 文件中的数字的代码吗?我无法替换其中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598380/

相关文章:

java - JMS如何使用JMS MessageListener获取多条消息

java泛型绑定(bind)不匹配: The type is not a valid substitute for the bounded parameter

java - 使用 Java 打开 Microsoft Word docx 文件

algorithm - 在 Go 中查找配对排列

haskell - 模式匹配 Data.Sequence 类似列表

java - 如何使用java中的colt库求解线性方程组

java - Java排序测试-

c# - StreamReader 在同时读取非常大的文件时性能不佳

Haskell:随机数 - IO 操作的 Int

enums - 匹配时修改枚举中的值