Why is System.out.print() not working?

42,794

Solution 1

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

Solution 2

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.

Share:
42,794
dwwilson66
Author by

dwwilson66

Visual Communications Professional with a background in technology. In the thick of learning Java, PHP & MySQL to augment my web skills. I'm taking a shine to programming a lot more than I thought I would.

Updated on April 26, 2020

Comments

  • dwwilson66
    dwwilson66 about 4 years

    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.