【问题标题】:Problems with reading .txt document and editing it in Java阅读 .txt 文档并在 Java 中编辑它的问题
【发布时间】:2016-01-07 01:06:03
【问题描述】:

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

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

线程“main”java.lang.Error 中的异常:未解决的编译问题: 此方法必须返回字符串类型的结果

这是我的代码:

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]);

    }
}

有谁知道是什么原因造成的?

【问题讨论】:

标签: java string compiler-errors


【解决方案1】:

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

b 无法解析为变量

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

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

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

【讨论】:

  • 是的,成功了!现在我回到了我遇到的第一个问题,我得到了错误消息:线程“main”java.lang.Error中的异常:未解决的编译问题:此方法必须返回String类型的结果你知道原因吗?这个?
  • if (s.equals("X")) { s = "0"; b.append(s).append(System.getProperty("line.separator")); b.toString(); } else { return b.toString(); } 如果 s 总是等于 X 该方法永远不会返回任何东西
  • 我知道输入文件包含几个 X 和几个整数。我只想让程序将所有 X 更改为 0,并将由此生成的整个字符串放入一个新的 .txt 文档中。我只是不明白这个错误信息。
  • 你知道,但是编译器不知道你可以在方法的末尾添加return null;,这将使它编译。
【解决方案2】:

Replace 中,您不会在所有情况下都返回字符串(或 null)

如果出现异常或文件没有内容则不返回任何值。

其次,您可能需要检查您的逻辑,以确定何时应该返回。目前如果s == "X"那么你会回来的。顺便说一句,这不是测试字符串相等性的正确方法。

【讨论】:

    【解决方案3】:

    捕获异常时必须返回一些东西(字符串对象或 null),或者将异常添加到方法签名而不是捕获它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多