【问题标题】:Illegal start of expression string表达式字符串的非法开头
【发布时间】:2014-06-11 17:27:20
【问题描述】:

我正在尝试制作一个反转文件中文本行的程序。我还在学习java,对此我很陌生。我的程序出错了,因为我在循环中创建了一个变量并试图在外部访问它。我尝试在声明字符串变量之前添加前缀“public”,但是当我尝试编译它时,它指向“public”并表示表达式的非法开始。有人可以告诉我为什么会出错以及如何解决它。

    import java.io.*;
    import java.util.*;

    public class FileReverser
    {
     public static void main(String[] args)
     throws FileNotFoundException
{
    Scanner console = new Scanner(System.in);
    System.out.print("File to Reverse: ");
    String inputFileName = console.next();

    System.out.print("Output File: ");
    String outputFileName = console.next();

    FileReader reader = new FileReader(inputFileName);
    Scanner in = new Scanner(reader);
    PrintWriter out = new PrintWriter(outputFileName);

    int number = 0;

    while (in.hasNextLine())
    {
        String line = in.nextLine();
        public String[] lines;
        lines[number] = line;
        number++;
    }
    int subtract = 0;
    for (int i;i>lines.length;i++)
    {
        out.println(lines[(lines.length-subtract)]);
        subtract++;
    }

    out.close();

    }
    }

【问题讨论】:

  • 首先要修复:缩进。正确缩进的代码使所有内容非常更容易理解。
  • 人们,请不要对此投反对票...他有一个真正的问题,如果其他人偶然发现这个问题,我们可以避免类似的问题。
  • @AnanthaSharma 很好的想法希望其他用户和你一样。

标签: java


【解决方案1】:

问题:

  • 您使用 public 修饰符声明 lines,该修饰符仅适用于实例/静态变量,不适用于局部变量。
  • 你永远不会初始化 lines
  • 您正在尝试在其范围之外使用lines(当前为while 循环)
  • 您正在尝试使用 i 而不对其进行初始化,这里:

    for (int i;i>lines.length;i++)
    
  • 您的if 条件错误;当ilines.length 时,您会想继续
  • subtract 最初为 0,因此访问 lines[lines.length - subtract] 将引发异常(因为它超出了数组的范围)

您可以使用以下代码修复这些问题:

// See note later
String[] lines = new String[1000];

while (in.hasNextLine()) {
    String line = in.nextLine();
    lines[number] = line;
    number++;
}
// Get rid of subtract entirely... and only start off at "number"
// rather than lines.length, as there'll be a bunch of null elements
for (int i = number - 1; i >= 0; i--) {
    out.println(lines[i]);
}

现在它最多可以处理 1000 行 - 但是有这个限制是很痛苦的。最好只使用List<String>:

List<String> lines = new ArrayList<String>();
while (in.hasNextLine()) {
    lines.add(in.nextLine());
}

然后您需要使用size() 而不是length,并使用get 而不是数组索引器来访问这些值 - 但它会是更简洁的代码 IMO。

【讨论】:

    【解决方案2】:

    这是一个范围问题。 lines 应该在 while 循环之外声明。通过将lines 放在while 循环中,它仅在该循环内可用。如果把它移到外面,lines 的作用域就会在 main 方法中。

    变量的作用域是程序中可以访问该变量的部分。

    这是伯克利大学关于变量和范围的讲义。如果你有时间,请阅读它。 http://www.cs.berkeley.edu/~jrs/4/lec/08

    【讨论】:

      【解决方案3】:

      访问修饰符(publicprivateprotected)仅适用于类成员(方法或字段)。

      如果您想访问受{} 限制的变量范围之外的变量,这意味着您在错误的范围内定义了变量。例如,如果您在内部定义了循环和变量x,然后想在循环外使用它:

      for (int i = 0; i < 10; i++) {
          .....
          int x = 6;
          .....
      }
      
      int y = x; // compilation error
      

      ...您实际上想在循环之前定义此变量:

      int x = 0;
      for (int i = 0; i < 10; i++) {
          .....
          x = 6;
          .....
      }
      
      int y = x; // not variable is available here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        • 2017-04-30
        • 1970-01-01
        • 2014-10-28
        • 2011-09-16
        相关资源
        最近更新 更多