Well to start off...here is my code.

import java.io.*;
import java.util.*;
// my name

public class project12
{
static Scanner console = new Scanner(System.in); 
Scanner inFile = new Scanner(new FileReader("f:\\JAVA\\projects\\receipts.txt")); 

public static void main(String[] args)
{
int movies;
double price;
price=2.00;
double change, cash, tax, total;

System.out.println("Enter how many movies you are renting.");
movies = console.nextInt();

System.out.println("movies = " + movies);
price = movies * 2.00;

System.out.println("The movie price is " price ".");

tax = price * .08;
System.out.println("Tax: " +tax);

total = price + tax;
System.out.println("The Total with Tax is " total ".");

System.out.println("Cash Tendered: ");
cash = console.nextDouble();

change = (cash - total);
System.out.printf("Your change is %.2f", change);

if (change < 5)

{System.out.println("Thank You For Doing Buisness With US.");}

else if (change >=5)
{System.out.println("Thank You");} 

else if (change >5);
{System.out.println("We Value Your Buisness Here At The Movie Shop");} 

inFile.close();

}
}

This is the error i am recieving:

project12.java:53: non-static variable inFile cannot be referenced from a static context
inFile.close();
^
1 error

does anyone have an idea on how to fix this. i have been trying for quite a while to no avail.

if anyone can help it would be much appreciated.

-silence-

Recommended Answers

All 3 Replies

You should use the code tags to make it easier to read your code. You will be able to get rid of that error, by declaring inFile as static, like you have done for console.


This is the error i am recieving:

project12.java:53: non-static variable inFile cannot be referenced from a static context
inFile.close();
^
1 error

re-write your code so that you are not calling it from a static context

if need be, create an object to do all that work for you, and call those methods (after instantiating that object) from within you main method

inFile is a non-static class variable while your main is a static method (which has to be in Java). And non-static instance variables cannot be accessed directly from static methods (to do so you will need to create an instance of the class and then access that instances inFile variable)

So you could either declare "inFile" as a static variable or a much better option would be just moving the declaration of inFile and console inside the main method.

BTW I really hope these are just your first steps in Java because you have actually reduced Java from a powerful Object Oriented Language to a Structured Language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.