dww*_*n66 6 java syntax system.out
所以我的编码很重要,但我认为这是一个相对简单的"读取文件"程序.我收到很多编译错误,所以我开始尝试一次编译一行,看看我在哪里被软管.这是我到目前为止的地方:
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: ");
}
Run Code Online (Sandbox Code Playgroud)
注意:这是从另一个类中的方法调用的前三行构造函数.其余的构造函数继续下面......当然没有上面的第二个大括号......
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;
}
Run Code Online (Sandbox Code Playgroud)
编译后,我得到以下内容:
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>
Run Code Online (Sandbox Code Playgroud)
什么在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!
}
Run Code Online (Sandbox Code Playgroud)
代码应该是这样的
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!
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据您的评论,这是您要实现的目标:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28695 次 |
| 最近记录: |