java中编译错误:找不到符号。 [英] Compile error in java :cannot find symbol.

查看:6656
本文介绍了java中编译错误:找不到符号。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码,它是一个文本文件,并把它变成一个字符串,然后将字符串的部分分成一个数组列表的不同元素。

I have this code that is taking a text file and turning it into a string and then separating parts of the string into different elements of an arraylist.

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;
            }
        }
    }
}

当我尝试编译这会给我两个错误:

However when I try to compile this it gives me two errors:


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

javac Message.java Message.java:31: cannot find symbol symbol : variable input location: class Message while(input.hasNextLine()){ ^ Message.java:32: cannot find symbol symbol : variable input location: class Message line = input.nextLine; ^

如何解决这个问题?我真的不知道是什么问题。

How do I fix this? I really don't know what's wrong.

我修复了,现在我的错误说
$ javac Message.java
Message.java:34 :找不到符号
symbol:variable nextLine
location:class java.util.Scanner
line = input.nextLine;
^

I fixed that and now my error says $ javac Message.java Message.java:34: cannot find symbol symbol : variable nextLine location: class java.util.Scanner line = input.nextLine; ^

           ^

现在有什么问题?

推荐答案

 Scanner input = new Scanner (inFile);

输入是构造函数的本地无法访问其外部,并且您尝试在 makeSmallerLists()方法中访问。使它成为一个实例成员,以便通过而不是 static 上下文可用。

input is local to the constructor, you cannot access outside of it, and you are trying to access in makeSmallerLists() method. Make it as a instance member, So that it available through out the class other than static context.

public class Grocery{

  Scanner input;

和构造函数

public Grocery(){

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

这篇关于java中编译错误:找不到符号。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆