user2787386
user2787386

Reputation: 303

Java System.out.println() throwing error

So I'm coming back to Java after a long time of not working with it. First method of my first class and I'm seeing an error I've never seen before.

For every System.out.println() statement I have, the .out. part throws this error: cannot find symbol symbol: variable out location: class System

my class is unfinished but looks like this

import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class System{
//Variables
char map[];
/*
Functions
FILE INPUT 
*/
public static void ReadFile(){
    FileInputStream fstream;
    try{
        fstream = new FileInputStream("C:\\Users\\James\\Documents\\NetBeansProjects\\Assignment1\\src\\testfiles");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;

        System.out.println("Your Input File");
        System.out.println("****************");

        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
            // Print the content on the console
            System.out.println(strLine);
            inputArray.add(strLine);
        }

        System.out.println("****************");
        //Close the input stream
        br.close();
        System.out.println();
    } 
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
  }
}

Every single .out. in this block of code throws this error :cannot find symbol symbol: variable out location: class System

I am using Netbeans8.0.2 and java 1.7.0_76(because I have to)

Can someone shed some light on this?

Upvotes: 1

Views: 3975

Answers (3)

Simmant
Simmant

Reputation: 1513

When you are using System.out.println() in the same class name System . So at the time of calling method println() your program searching the method in same class instead of checking same in java.lang. package.

So as for the solution of issue , either you can change the name of class to some thing else rather than System or you can change System.out.println() with java.lang.System.out.println().

Upvotes: 1

Raghav Sood
Raghav Sood

Reputation: 82543

Replace all the System.<something> with java.lang.System.<something>.

In its current state, your code is referencing your own System class. Since the name is the same, and yours has a higher priority in the scope, you end up with this error.

It's probably a better idea to change the name of your class. You generally don't want to conflict with internal names.

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1500225

This is the problem:

public class System

You're creating your own class called System, so when you later use:

System.out.println

that's looking in your System class rather than java.lang.System.

Options:

  • Change the name of your class. It's generally a bad idea to create classes with the same name as classes within java.lang, precisely for this reason
  • Fully-qualify the call:

    java.lang.System.out.println(...);
    

I'd pick the former option, personally.

Upvotes: 11

Related Questions