【问题标题】:Compile error in java :cannot find symbol. [duplicate]java中的编译错误:找不到符号。 [复制]
【发布时间】:2013-12-09 19:10:06
【问题描述】:

我有这段代码,它获取一个文本文件并将其转换为一个字符串,然后将部分字符串分成一个数组列表的不同元素。

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class Grocery{

    public Grocery(){

        File inFile = new File ("lists.txt");
        Scanner input = new Scanner (inFile);
        String grocery;
        {
             grocery = input.nextLine();
        }
    }

    public void makeSmallerLists(){
        String listLine;
        String line;
        ArrayList<String> smallList = new ArrayList<String>();
        while(input.hasNextLine()){
            line = input.nextLine;
            if(line.equals("<END>")){
                smallList.add(listLine);
            } else{
                listLine = listLine + "\n" + line;
            }
        }
    }
}

但是,当我尝试编译它时,它给了我两个错误:

javac Message.java Message.java:31:找不到符号符号: 变量输入位置:类 Message while(input.hasNextLine()){ ^ Message.java:32:找不到符号符号:变量输入位置:类消息行 = input.nextLine; ^

我该如何解决这个问题?我真的不知道怎么了。

我解决了这个问题,现在我的错误说 $ javac 消息.java Message.java:34:找不到符号 符号:变量 nextLine 位置:类 java.util.Scanner 线=输入。下一条线; ^

           ^

现在出了什么问题?

【问题讨论】:

    标签: java variables input compiler-errors


    【解决方案1】:
     Scanner input = new Scanner (inFile);
    

    input 在构造函数中是本地的,你不能在它之外访问,你试图在makeSmallerLists() 方法中访问。使其成为实例成员,以便它在 class 以外的 static 上下文中可用。

    public class Grocery{
    
      Scanner input;
    

    在构造函数中

    public Grocery(){
    
                File inFile = new File ("lists.txt");
                 input = new Scanner (inFile);
    

    【讨论】:

      【解决方案2】:

      您有变量范围问题。您无法访问范围之外的字段。将 Scanner 声明为全局的,在 costructor 之外。

       public class Grocery{
          Scanner input = null;// Declare Scanner here.
      
          public Grocery(){
              .....
              input=new Scanner (inFile);
          }
      

      还附加方法括号()

            public void makeSmallerLists(){
             ......
              while(input.hasNextLine()){
              line = input.nextLine();// Append () after method. 
             .....
            }
      

      【讨论】:

        【解决方案3】:

        这是因为 Scanner 对象 input 已在您的构造函数中声明(构造函数的本地范围),因此它在您的 makeSmallerLists() 中不可见。您需要将其声明为实例变量,以便在该类的所有方法中都可以访问它。

        public class Grocery {
            Scanner input; // declared here, as an instance variable
            public Grocery(){
                File inFile = new File ("lists.txt");
                input = new Scanner (inFile);  // initialized here
                ...
            }
            ...
            public void makeSmallerLists() {
                ...
                while(input.hasNextLine()) { // accessible everywhere within the class
                ...
            }
        }
        

        【讨论】:

          【解决方案4】:

          一种解决方案是使用Scanner 类型的class member

          private Scanner input;
          

          并在构造函数中构造它:

          public class Grocery { 
             private Scanner input;
          
             public Grocery() {
                ...
                ...
                input = new Scanner (inFile);
             }
             ...
          }
          

          现在input 不限于构造函数的范围,它可以通过整个类访问。


          考虑这个例子:

          public class Example {
              int n = 0;  //n known everywhere in the class
          
              if(n == 0) {
                 int n1 = 1;
                 if(n1 == 1) {
                    int n2 = 2;
                    //n1 is known here
                 }
                 //n2 is not known here
              } 
              //n1 is not known here
          }
          

          【讨论】:

            【解决方案5】:

            input 在构造函数之外是不可访问的...它在构造函数内部声明

            【讨论】:

              猜你喜欢
              • 2012-08-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-31
              • 2013-12-06
              • 2013-03-08
              • 1970-01-01
              • 2013-10-04
              相关资源
              最近更新 更多