[英]Why is System.out.print() not working?
所以我的編碼很重要,但我認為這是一個相對簡單的“讀取文件”程序。 我收到很多編譯錯誤,所以我開始嘗試一次編譯一行,看看我在哪里被軟管。 這是我到目前為止的地方:
import java.nio.file.*;
import java.io.*;
import java.nio.file.attribute.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.text.*;
//
public class ReadStateFile
{
Scanner kb = new Scanner(System.in);
String fileName; /* everything through here compiles */
System.out.print("Enter the file to use: ");
}
注意:這是從另一個類中的方法調用的前三行構造函數。 其余的構造函數繼續下面......當然沒有上面的第二個大括號......
fileName = kb.nextLine();
Path file = Paths.get(fileName);
//
final String ID_FORMAT = "000";
final String NAME_FORMAT = " ";
final int NAME_LENGTH = NAME_FORMAT.length();
final String HOME_STATE = "WI";
final String BALANCE_FORMAT = "0000.00";
String delimiter = ",";
String s = ID_FORMAT + delimiter + NAME_FORMAT + delimiter + HOME_STATE + delimiter + BALANCE_FORMAT + System.getProperty("line.separator");
final int RECSIZE = s.length();
//
byte data[]=s.getBytes();
final String EMPTY_ACCT = "000";
String[] array = new String[4];
double balance;
double total = 0;
}
編譯后,我得到以下內容:
E:\java\bin>javac ReadStateFile.java
ReadStateFile.java:20: error: <identifier> expected
System.out.print("Enter the file to use: ");
^
ReadStateFile.java:20: error: illegal start of type
System.out.print("Enter the file to use: ");
^
2 errors
E:\java\bin>
什么在HECK中我錯過了? 並且有人可以向我發送一段代碼來產生堆棧跟蹤嗎? 我只是迷惑自己閱讀java文檔,Java Tutotrials甚至沒有“堆棧”作為索引關鍵字。 Hrmph。
你不能讓代碼在這樣的類中漂浮。 它需要在方法,構造函數或初始化程序中。 您可能想在主方法中使用該代碼。
在聲明類的屬性/方法時,不能使用方法。
public class ReadStateFile
{
Scanner kb = new Scanner(System.in);
String fileName; /* everything through here compiles */
System.out.print("Enter the file to use: "); //wrong!
}
代碼應該是這樣的
public class ReadStateFile
{
Scanner kb = new Scanner(System.in);
String fileName; /* everything through here compiles */
public void someMethod() {
System.out.print("Enter the file to use: "); //good!
}
}
編輯:根據您的評論,這是您要實現的目標:
public class ReadStateFile
{
public ReadStateFile() {
Scanner kb = new Scanner(System.in);
String fileName; /* everything through here compiles */
System.out.print("Enter the file to use: ");
//the rest of your code
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.