![](/img/trans.png)
[英]Class interface or enum expected error in temperature lab. Please check and see what I did wrong and explain
[英]Compiler keeps spitting "Class Interface or Enum Expected" can anyone spot what I've done wrong?
我的主程序似乎有問題,但我無法弄清楚。 我是java新手,很累。 如果有幫助,請使用 Netbeans 7.3.1
package Retro;
import java.io.*;
import static java.lang.System.*;
main class Retro {
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args) {
try {
FileReader readfile = new FileReader("blah.txt");
BufferedReader bf = new BufferedReader(readfile);
String str;
while ((str = bf.readLine()) != null) {
out.println(str + "\n");
}
bf.close();
} catch (IOException e) {
out.println("File not found");
}
}
}
main class Retro
部分將無法編譯。
main
不是 Java 關鍵字。
只需將其刪除。
您只需要一個名為main
的static
方法,帶有String[]
或 varargs String...
簽名並返回void
以獲得可執行類。
小注:包名應該小寫。
package Retro;
import java.io.*;
import static java.lang.System.*;
public class Retro {
@SuppressWarnings("ConvertToTryWithResources")
public static void main (String[] args){
try {
FileReader readfile = new FileReader ("blah.txt");
BufferedReader bf = new BufferedReader (readfile);
String str;
while ((str = bf.readLine()) != null){
out.println(str + "\n");
}
bf.close();
} catch (IOException e) {
out.println("File not found");
}
}
}
你把 main 放在 class 之前,編譯器不知道“main”是什么。
您應該刪除包聲明(第一行)和關鍵字 main,然后我想您會發現它可以編譯和運行。 像這樣的東西:
import java.io.*;
import static java.lang.System.*;
public class Retro {
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args) {
try {
FileReader readfile = new FileReader("blah.txt");
BufferedReader bf = new BufferedReader(readfile);
String str;
while ((str = bf.readLine()) != null) {
out.println(str + "\n");
}
bf.close();
} catch (IOException e) {
out.println("File not found");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.