简体   繁体   English

为什么System.out.print()不起作用?

[英]Why is System.out.print() not working?

So I'm in the thick of coding what I though would be a relatively simple "read file" program. 所以我的编码很重要,但我认为这是一个相对简单的“读取文件”程序。 I am getting LOTS of compile errors, so I started just trying to compile one line at a time to see where I was getting hosed. 我收到很多编译错误,所以我开始尝试一次编译一行,看看我在哪里被软管。 Here's where I am so far: 这是我到目前为止的地方:

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: ");
}

NOTE: This is the first three lines of constructor that's called from a method in another class. 注意:这是从另一个类中的方法调用的前三行构造函数。 The rest of the constructor continues below...without the second curly brace above, of course... 其余的构造函数继续下面......当然没有上面的第二个大括号......

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;
}

Upon compilation, I get the following: 编译后,我得到以下内容:

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>

What in the HECK am I missing? 什么在HECK中我错过了? and could someone shoot me a snippet of code to produce a stack trace? 并且有人可以向我发送一段代码来产生堆栈跟踪吗? I just confused myself reading the java documentation, and the Java Tutotrials don't even have "stack" as an indexed keyword. 我只是迷惑自己阅读java文档,Java Tutotrials甚至没有“堆栈”作为索引关键字。 Hrmph. Hrmph。

You cannot have code just floating around in a class like that. 你不能让代码在这样的类中漂浮。 It either needs to be in a method, a constructor, or an initializer. 它需要在方法,构造函数或初始化程序中。 You probably meant to have that code in your main method. 您可能想在主方法中使用该代码。

You can't use a method while declaring the attributes/methods for a class. 在声明类的属性/方法时,不能使用方法。

public class ReadStateFile
{
    Scanner kb = new Scanner(System.in);
    String fileName;     /* everything through here compiles */
    System.out.print("Enter the file to use: "); //wrong!
}

The code should be something like this 代码应该是这样的

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!
    }
}

EDIT: based in your comment, this is what you're trying to achieve: 编辑:根据您的评论,这是您要实现的目标:

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.

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