【问题标题】:Public string - cannot be resolved公共字符串 - 无法解析
【发布时间】:2014-01-03 01:10:49
【问题描述】:

发生错误

bw.write(dataString);

我该如何解决这个问题?
dataString 无法解析为变量。

public class test {
        public static void main(String[] args){
            ArrayList<String> data = new ArrayList<String>();

            try (BufferedReader br = new BufferedReader(new FileReader("src/test.txt"))) {
            String CurrLine;

            while((CurrLine = br.readLine()) != null) {
                data.add(CurrLine);
            }
            String[] dataArray = new String[data.size()];
            String dataString = Arrays.toString(dataArray);

            String[] client = dataString.split("<::>");
            Integer nameId = Arrays.binarySearch(client, "Test");
            Integer versId = nameId + 1;
            System.out.println(client[nameId] + "\n" + client[versId]);


        } catch(FileNotFoundException ex) {
        System.out.println("FNFE");
        } catch(IOException ex) {
            System.out.println("IOE");
        }
            try{
                File file = new File("src/test.txt");
                if (!file.exists()) {
                file.createNewFile();
                }
                FileWriter fw = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(fw);
        bw.write(dataString);
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

【问题讨论】:

  • 你需要了解变量的作用域dataString 的范围仅限于 try-catch 块。

标签: java arrays string try-catch public


【解决方案1】:

在 try and catch 块之外声明dataString... 仅此而已。 ;) 如果你在循环中声明它,或者在这种情况下是你的 try catch 块,它的生命周期仅限于它。

像这样:

String dataString = null;

在 try-catch 块内:

dataString = Arrays.toString(dataArray);

【讨论】:

  • 如何从 dataString 中获取数据?
  • 用一个例子编辑了我的答案。第一行代码必须在 try-catch 块之外
  • 我收到错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: -1 at test.main(test.java:33)
  • 我在这条线上打赌:System.out.println(client[nameId] + "\n" + client[versId]);。只需调试并查找问题所在。
【解决方案2】:

dataString 超出了 try 块的范围。

也许将 dataString 作为实例变量添加到类的顶部。

public class test {
        private String dataString = null;
        public static void main(String[] args){
            ArrayList<String> data = new ArrayList<String>();

            try (BufferedReader br = new BufferedReader(new FileReader("src/test.txt"))) {
            String CurrLine;

            while((CurrLine = br.readLine()) != null) {
                data.add(CurrLine);
            }
            String[] dataArray = new String[data.size()];
            dataString = Arrays.toString(dataArray);
         ...

【讨论】:

  • 我收到错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: -1 at test.main(test.java:33)
【解决方案3】:

dataString 变量的范围仅限于第一个 try-catch 块。将其声明更改如下,

public static void main(String[] args){
    ArrayList<String> data = new ArrayList<String>();
    String dataString = null;

    try (BufferedReader br = new BufferedReader(new FileReader("src/test.txt"))) {
        String CurrLine;

        while((CurrLine = br.readLine()) != null) {
            data.add(CurrLine);
        }
        String[] dataArray = new String[data.size()];
        dataString = Arrays.toString(dataArray);

        String[] client = dataString.split("<::>");
        Integer nameId = Arrays.binarySearch(client, "Test");
        Integer versId = nameId + 1;
        System.out.println(client[nameId] + "\n" + client[versId]);


    } catch(FileNotFoundException ex) {
        System.out.println("FNFE");
    } catch(IOException ex) {
        System.out.println("IOE");
    }
    try{
        File file = new File("src/test.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(dataString);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我得到错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: -1 at test.main(test.java:33)
  • 您可能正在搜索数组中不存在的元素。如果没有与给定项相等的项,则搜索返回 -1。
  • 另外,根据javadoc,数组必须在调用二分搜索之前进行排序。如果不是,您必须进行线性搜索才能找到索引。
  • 我的数组中有这个元素。如果我初始化 dataString 我得到了这个错误,如果我删除写代码并在 try-catch 中初始化 dataString 一切正常。对不起我的英语不好。
  • 数组排序了吗?在Integer nameId = Arrays.binarySearch(client, "Test");这一行调试检查,确保元素在数组中,并且数组是排序好的。
猜你喜欢
  • 2020-05-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2021-07-24
  • 2021-04-24
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多