java - 读取.txt文档并在Java中进行编辑时出现问题

标签 java string compiler-errors

我一直在研究一个程序,该程序读取一个文件,然后找到所有X,并将它们返回为0,以便可以在完整程序中使用它。

到目前为止,我在正确转换为字符串方面遇到许多问题。我目前收到错误消息:

线程“主”中的异常java.lang.Error: Unresolved 编译问题:
此方法必须返回String类型的结果

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class ReadMagicSquare {

    public static String Replace(String filename) {
        try {
            Scanner file = new Scanner(new File(filename));
            System.out.println(file);
            StringBuilder b = new StringBuilder();
            String s = "";
            int n = 0;
            while(file.hasNextLine()){

                s = file.nextLine();

                n++;

                if (s == "X") {
                    s = "0";
                    b.append(s).append(System.getProperty("line.separator"));
                    return b.toString();
                } else {
                    return b.toString();
                }

                }

        } catch (Exception e) {

        }
    }

    static int[][] matrix;
    int size = -1;
    int log10 = 0;
    String numberFormat;

    public ReadMagicSquare(String filename) {
        try {
            readFile("test1.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readFile(String filename) throws IOException {

        BufferedReader John= new BufferedReader(new FileReader("test1.txt"));

        String line;
        int j = 0;

        while ((line = John.readLine()) != null) {
            String[] vals = line.trim().split("\\s+");

            if (matrix == null) {
                size = vals.length;
                matrix = new int[size][size];
                log10 = (int) Math.floor(Math.log10(size * size)) + 1;
                numberFormat = String.format("%%%dd", log10);
            }

            for (int i = 0; i < size; i++) {
                matrix[j][i] = Integer.parseInt(vals[i]);
            }

            j++;
        }
    }

    public String toString() {
        StringBuffer John1 = new StringBuffer();

        if (matrix != null) {
            for (int j = 0; j < size; j++) {
                John1.append(" ");
                for (int i = 0; i < size; i++) {
                    John1.append(String.format(numberFormat,  matrix[j][i]));

                }
                if (j < size - 1) {

                    John1.append("\n");
                } else {
                    John1.append("");
                }
            }
        }

        return John1.toString();
    }
    public static void main(String[] args) {
        Replace("test1.txt");    
        ReadMagicSquare square = new ReadMagicSquare("square.txt");
        System.out.println(square.toString());
        System.out.println(matrix[3][0]);

    }
}

你们当中有人知道原因吗?谢谢你的时间!

编辑:

非常感谢您的投入!我进行了以下更改,但仍然有问题:
public static String Replace(String filename) {
    try {
        Scanner file = new Scanner(new File(filename));
        System.out.println(file);
        StringBuilder b = new StringBuilder();
        String s = "";
        int n = 0;
        while(file.hasNextLine()){

            s = file.nextLine();

            n++;

            if (s.equals("X")) {
                s = "0";
                b.append(s).append(System.getProperty("line.separator"));
                b.toString();
            } else {
                return b.toString();
            }

            }

    } catch (Exception e) {
        return null;
    }
    try (PrintStream out = new PrintStream(new   FileOutputStream("test1,1.txt"))) {
        out.print(b);
    }
}

我现在对out.print(b)有问题;线。它给了我错误:

b无法解析为变量

这对我来说是新的。有人愿意详细说明吗?

编辑2:

现在,我已经解决了大多数问题,但是当我尝试运行该程序时,它不会更改.txt文件中的X。我得到相同的输出,而我希望所有X都变为0。我现在有以下代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;


public class ReadMagicSquare {

    static int[][] matrix;
    int log10 = 0;
    String numberFormat;
    String line;
    int j = 0;
    int size = 0;

    public static String Replace(String filename) {
        try {
            Scanner file = new Scanner(new File(filename));
            System.out.println(file);
            StringBuilder b = new StringBuilder();
            String s = "";
            while(file.hasNextLine()){

                s = file.nextLine();


                if (s.equals("X")) {
                    s = "0";
                    b.append(s).append(System.getProperty("line.separator"));
                } else {
                    b.append(s).append(System.getProperty("line.separator"));
                }

                }
            try (PrintStream out = new PrintStream(new FileOutputStream("test1,1.txt"))) {
                out.print(b);
                out.close();
            } catch (IOException e) { 

            } 

        } catch (Exception e) {
            System.out.println("Problem reading file.");
        }
      return "bobby";
    }

    public ReadMagicSquare(String filename) {
        try {
            readFile("test1,1.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readFile(String filename) throws IOException {

        BufferedReader buffer = new BufferedReader(new FileReader("test1,1.txt"));



        while ((line = buffer.readLine()) != null) {
            String[] vals = line.trim().split("\\s+");

            if (matrix == null) {
                size = vals.length;
                matrix = new int[size][size];
                log10 = (int) Math.floor(Math.log10(size * size)) + 1;
                numberFormat = String.format("%%%dd", log10);
            }

            for (int i = 0; i < size; i++) {
                matrix[j][i] = Integer.parseInt(vals[i]);
            }

            j++;
        }
    }

    public String toString() {
        StringBuffer buff = new StringBuffer();

        if (matrix != null) {
            for (int j = 0; j < size; j++) {
                buff.append(" ");
                for (int i = 0; i < size; i++) {
                    buff.append(String.format(numberFormat,  matrix[j][i]));

                }
                if (j < size - 1) {

                    buff.append("\n");
                } else {
                    buff.append("");
                }
            }
        }

        return buff.toString();
    }

    public static void main(String[] args) {
        Replace("test1.txt");
        ReadMagicSquare square = new ReadMagicSquare("square.txt");
        System.out.println(square.toString());
        System.out.println(matrix[3][0]);

    }
}

有人知道导致问题的原因吗?

最佳答案

I now have issues with the out.print(b); line. It gives me the error:

b cannot be resolved to a variable

which is new to me. Would anyone care to elaborate?



那是因为您不能访问catch块中try块内部声明的变量,只需将变量声明移到try块之前(您仍然可以在try块内部对其进行初始化)。

这是同一问题的另一个答案https://stackoverflow.com/a/2854153/1935341

关于java - 读取.txt文档并在Java中进行编辑时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645714/

相关文章:

java - for语句中的编译器错误?

java - 对话范围 CDI 与 Ajax

java - 如何在 selenium webdriver 中找到动态元素的 Xpath

java - 如何使用 openCV 检测视频中的运动

java - 为什么 javamail API 有一个消息发件人数组?

java - 找不到符号方法 get(int)

ruby - Ruby 如何比较字符串?

php - 如果条件字符串,如何通过 php 获取特定值

c - 处理结构中的字符串

c++ - POCO dev-release v1.5.3 编译失败