Why is System.out.print() not working?
Asked Answered
N

2

7

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.

Niggerhead answered 26/4, 2012 at 1:0 Comment(2)
Please edit your question to show exactly what your constructor looks like. Trying to find syntax errors when you're not even looking at the actual code is going to be a waste of time.Entresol
@Entresol - just posted the other bits of the constructorNiggerhead
C
9

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
    }
}
Capsulate answered 26/4, 2012 at 1:1 Comment(3)
I forgot--and just added-- that this is the first part of a constructor. Do I still need to use a method in this class? or will calling this constructor from a method in another class do the same thing?Niggerhead
@Niggerhead The thing here is that your code is not in the constructor but in the class definition.Capsulate
@Luigi BINGO! Inherited code, lack of sleep. I feel like an idiot now. Ugh. Thanks.Niggerhead
E
6

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.

Escarole answered 26/4, 2012 at 1:2 Comment(2)
@Jeffrey...just clarified; This is the first part of a constructor.Niggerhead
@Niggerhead Then post a snippet with that constructorEscarole

© 2022 - 2024 — McMap. All rights reserved.