Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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. Hrmph.

like image 394
dwwilson66 Avatar asked Apr 26 '12 01:04

dwwilson66


1 Answers

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
    }
}
like image 169
Luiggi Mendoza Avatar answered Oct 30 '22 10:10

Luiggi Mendoza