一、问题描述
在Java源代码文件中,使用 System.out.println() 语句进行输出,编译器提示“Cannot resolve symbol ‘println’(无法解释关键字)”, println飘红。报错代码及报错截图如下所示。
import java.io.*;
public class String_Search
{
// Wrong code
String str = "Geeks For Geeks is a computer science portal";
int first_in = str.indexOf('s', 10);
System.out.println("First occurrence of char 's' after index 10: " + first_in);
}
二、解决过程
Control键点击out,可见电脑里有输出流out的定义,如下图所示:
出错原因在于没有将这几行代码放在主函数main当中,稍作修改,注释掉之前的错误代码,报错消失,成功运行:
import java.io.*;
public class String_Search
{
public static void main(String [] args)
{
String str = "Geeks For Geeks is a computer science portal";
int first_in = str.indexOf('s', 10);
System.out.println("First occurrence of char 's' after index 10: " + first_in);
}
/*
// Wrong code
String str = "Geeks For Geeks is a computer science portal";
int first_in = str.indexOf('s', 10);
System.out.println("First occurrence of char 's' after index 10: " + first_in);
*/
}