简体   繁体   English

out.println 说 out 无法解决

[英]out.println says out cannot be resolved

When I write out.println() , eclipse complains that out cannot be resolved.当我写out.println()时,eclipse 抱怨 out 无法解决。

I have imported java.io.* and other servlet packages.我已经导入了java.io.*和其他 servlet 包。

Just a shot in the dark, I think this is the out you are looking for:在黑暗中只是一个镜头,我认为这是out你正在寻找:

public class OutServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("foo");
    }
}

You should write System.out.println();你应该写System.out.println(); inside a function.在一个函数里面。

If you write it directly into the class then it might show the error that you are having right now.如果您直接将其写入类中,那么它可能会显示您现在遇到的错误。

采用:

import static java.lang.System.out;

Import it using a static import:使用static导入导入它:

    import static java.lang.System.out;

However, I'd recommend that you don't do this.但是,我建议您不要这样做。

  • Using the full name makes references to System.out stand out, and makes them easier to "grep" for ... if you need to nuke traceprints.使用全名会使对System.out引用脱颖而出,并使它们更容易“grep”...如果您需要核对痕迹。

  • If you need to write a lot of stuff to the console, you should make out a variable or a method parameter.如果你需要很多的东西写到控制台,你应该做out的变量或方法参数。 This will help to make your code more reusable;这将有助于使您的代码更可重用; eg so that it can write to somewhere other than System.out .例如,以便它可以写入System.out以外的其他地方。

I had this problem.我有这个问题。 I found out I didn't have main method( public static void main(string[]args) and that is why it was giving me this error. Hope this helps.我发现我没有 main 方法( public static void main(string[]args) 这就是为什么它给我这个错误。希望这会有所帮助。

If your are looking to put some debug outputs, you can do this:如果您想放置一些调试输出,您可以这样做:

System.out.println("foo");

If your are looking for adding the output to HTML not printing on debug console you can do as follow: First you should add the 'servlet-api.jar' to your project.如果您正在寻找将输出添加到不在调试控制台上打印的 HTML,您可以执行以下操作:首先,您应该将“servlet-api.jar”添加到您的项目中。 Then simply you can use this if you want to add your output to HTML:然后,如果您想将输出添加到 HTML,您可以使用它:

response.getWriter().println("foo");

I hope it helps.我希望它有帮助。

Can you please check whether you are having any class name as "System" ?你能检查一下你是否有任何类名作为“系统”吗? This could be the one reason.这可能是一个原因。 Like in the code below if I use class name as System it would end up with the same Error like yours.就像下面的代码一样,如果我使用类名作为 System 它最终会出现与您相同的错误。

********** Cannot use Identifier 'System' as Class name;********** ********** 不能使用标识符“系统”作为类名;**********

public class System {

public static void main(String[] args) {

    int A[] = { 3, 9, 7, 8, 12, 6, 15, 5, 4, 10 };

    for (int x : A) {
        System.out.print(x + ", ");

    }
    System.out.println("\n*******************");
    int t = A[A.length - 1];

    System.out.println(t);

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM