值之间没有空格 txt java

发布于 2025-01-19 05:08:36 字数 1241 浏览 0 评论 0原文

代码工作正常,除了一个问题。加薪后,值之间的空间消失了,然后我的程序就无法正常工作。

Joe 2022/04/05 HR-Manager44200
Steve 2022/04/06 Admin 100000
Scanner console = new Scanner(System.in); 
System.out.print("Name of employee : "); 
String pID = console.nextLine(); System.out.print("Allowance : "); 
replenish = console.nextInt();    
File originalFile = new File("worker.txt");
    BufferedReader br = new BufferedReader(new FileReader(originalFile));

    File tempFile = new File("tempfile.txt");
    PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

    String line = null;
    while ((line = br.readLine()) != null) {

        if (line.contains(pID)) {
            String strCurrentSalary = line.substring(line.lastIndexOf(" "));
            if (strCurrentSalary != null || !strCurrentSalary.trim().isEmpty()) {
                int replenishedSalary = Integer.parseInt(strCurrentSalary.trim()) + replenish;
                System.out.println("Sum with added : " + replenishedSalary);
                line = line.substring(0, line.lastIndexOf(" ")) + replenishedSalary;
            }

        }
        pw.println(line);
        pw.flush();
    }
    pw.close();
    br.close();

我想知道问题出在哪里以及为什么空间没有增加

Code works fine, except one problem. After increasing salary the spaces beetwen values disapears and then my programm doesn't work well.

Joe 2022/04/05 HR-Manager44200
Steve 2022/04/06 Admin 100000
Scanner console = new Scanner(System.in); 
System.out.print("Name of employee : "); 
String pID = console.nextLine(); System.out.print("Allowance : "); 
replenish = console.nextInt();    
File originalFile = new File("worker.txt");
    BufferedReader br = new BufferedReader(new FileReader(originalFile));

    File tempFile = new File("tempfile.txt");
    PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

    String line = null;
    while ((line = br.readLine()) != null) {

        if (line.contains(pID)) {
            String strCurrentSalary = line.substring(line.lastIndexOf(" "));
            if (strCurrentSalary != null || !strCurrentSalary.trim().isEmpty()) {
                int replenishedSalary = Integer.parseInt(strCurrentSalary.trim()) + replenish;
                System.out.println("Sum with added : " + replenishedSalary);
                line = line.substring(0, line.lastIndexOf(" ")) + replenishedSalary;
            }

        }
        pw.println(line);
        pw.flush();
    }
    pw.close();
    br.close();

I want to know where is the problem and why space doesn't adding

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2025-01-26 05:08:36

您正在丢失空间,因为 line.substring(0, line.lastIndexOf(" ")) 将返回直到最后一个空格的子字符串,不包括空格,因此您可以在 line 之后添加空格.substring(0, line.lastIndexOf(" ")) 如下所示。

line = line.substring(0, line.lastIndexOf(" ")) + " " + replenishedSalary;

您也可以使用 String.format 创建如下行。

line = String.format("%s %d",line.substring(0, line.lastIndexOf(" ")),replenishedSalary)

You are losing space because line.substring(0, line.lastIndexOf(" ")) will return a substring up to the last space not including the space so you could add a space after line.substring(0, line.lastIndexOf(" ")) like below.

line = line.substring(0, line.lastIndexOf(" ")) + " " + replenishedSalary;

Also you can use String.format to create the line as below.

line = String.format("%s %d",line.substring(0, line.lastIndexOf(" ")),replenishedSalary)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文