4 Replies - 11036 Views - Last Post: 03 February 2010 - 08:27 PM Rate Topic: -----

#1 ala_heu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 09

non-static variable cannot be referenced from a static context

Posted 28 January 2010 - 10:05 PM

I'm taking a Java level 2 course, but I have no previous Java experience (only C++, because the prerequisite was either the Java OR the C++ introductory course). I know how to write this in C++, but the Java solution is required, so the code may be riddled with little errors, but the only errors that occur from compiling are all the same:

"non-static variable scan cannot be referenced from a static context"

They refer to the three "System.out.println" lines in the code. Any help/explanations would be much appreciated.

import java.util.Scanner;
import java.text.NumberFormat;

public class Assignment2
{
	double[] numbers;
	int count, negCount = 0;
	double minNum, positiveSum = 0;
	Scanner scan = new Scanner(System.in);
	
	public static void main (String[] args)
	{
		while (scan.nextDouble() != 0)
		{
			numbers[count] = scan.nextDouble();
			count++;
		}
		
		NumberFormat fmtDollar = NumberFormat.getCurrencyInstance();
		
		System.out.println ("The minimum number is " + Assignment2.findMin(numbers[])); //??????
		System.out.println ("The sum of the positive numbers is " + fmtDollar.format(Assignment2.computePositiveSum(numbers[]))); //??????
		System.out.println ("The total number of negative numbers is " + Assignment2.countNegative(numbers[])); //??????
	}
	
	public static double findMin(double[] numbers, int count) //count is the count of numbers stored in the array
	{
		int minNum = numbers[0];
		
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] < minNum)
				minNum = numbers[count];
		}
		
		return minNum;
	}
	
	public static double computePositiveSum(double[] numbers, int count)
	{
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] > 0)
				positiveSum = positiveSum + numbers[count];
		}
		
		return positiveSum;
	}
	
	public static int countNegative(double[] numbers, int count)
	{
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] < 0)
				negCount++;
		}
		
		return negCount;
	}
}


This post has been edited by ala_heu: 28 January 2010 - 10:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: non-static variable cannot be referenced from a static context

#2 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: non-static variable cannot be referenced from a static context

Posted 28 January 2010 - 10:26 PM

A static variable is a variable which can be accessed when an object has not been initialized (instantiated). Non static variables can be accessed only via an instance of the object. When you start your instruction by "Assignment2.", it means you are referencing a static variable, because Assignment2 is the NAME of the object, and not an instance. Try declaring the variables static (the ones you are willing to access).
Was This Post Helpful? 0
  • +
  • -

#3 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: non-static variable cannot be referenced from a static context

Posted 28 January 2010 - 11:17 PM

There seems to be a couple of errors here:

1) The static problem.
This basically says that any method declared as static can only use static variables unless they are local.

You are seeing this error because of this code

public class Assignment2
{
	double[] numbers;
	int count, negCount = 0;
	double minNum, positiveSum = 0;
	Scanner scan = new Scanner(System.in);
	
	public static void main (String[] args)
	{
		while (scan.nextDouble() != 0)
		{
			numbers[count] = scan.nextDouble();
			count++;
		}
...
	}
}


Notice that you have a class definition of 'Assignment2' with several member variables.
This class also happens to have a static method called main(). Now main can only use
statically declared variables if they are declared outside of the static method. For you this means
to either delclare scan as static Scanner scan = new Scanner(System.in) or move it
inside of the main method.


The other problem I see is with your print statements
System.out.println ("The minimum number is " + Assignment2.findMin(numbers[])); //??????


Passing arrays to methods in java is pretty similar to c/c++. You do not need to include '[]'.
Furthermore, the findMin method requires 2 parameters. You should just be able to remove count
from the method decleration as java arrays know their length.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: non-static variable cannot be referenced from a static context

Posted 29 January 2010 - 09:32 AM

Hey, my first blog entry is all about static methods and variables. I urge you to tread through it and it will clear some things up. http://www.dreaminco...?showentry=2019
Was This Post Helpful? 0
  • +
  • -

#5 ala_heu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 09

Re: non-static variable cannot be referenced from a static context

Posted 03 February 2010 - 08:27 PM

Thanks all, it turned out that we just hadn't covered the concept of static vs instance variables and methods in lecture yet. Here's an update. I got the program up and running, however it doesn't work for one of the test cases (assignment is completed and graded, so I'm just looking for some closure). The test case that it fails on is below the code:

import java.util.Scanner;
import java.text.NumberFormat;

public class Assignment2
{
	public static void main (String[] args)
	{
		// Input array initialized with 100 slots (name: numbers)
		double[] numbers = new double[100];
		int count = 0;
		double minNum;
		Scanner scan = new Scanner(System.in);
		
		// num initialized to scan the next double inputted
		double num = scan.nextDouble();
		
		// num is used to write the input into an array until the number 0 is encountered
		while (num != 0)
		{
			numbers[count] = num;
			num = scan.nextDouble();
			count++;
		}
		
		// fmtDollar and fmtInteger are defined to help manage the output format
		NumberFormat fmtDollar = NumberFormat.getCurrencyInstance();
		NumberFormat fmtInteger = NumberFormat.getIntegerInstance();
		
		// The minimum number (line 40), sum of positive numbers (line 41) and number of negative values (line 42)
		// are all output in their respective formats
		System.out.println ("The minimum number is " + fmtInteger.format(Assignment2.findMin(numbers, 0)));
		System.out.println ("The sum of the positive numbers is " + fmtDollar.format(Assignment2.computePositiveSum(numbers, 0)));
		System.out.println ("The total number of negative numbers is " + Assignment2.countNegative(numbers, 0));
	}
	
	// findMin determines the smallest valued number of all the numbers within the array numbers
	public static double findMin(double[] numbers, int count)
	{
		double minNum = numbers[count];
		
		// The following for loop determines whether the previous array slot is less than the current one.
		// If the current one is lower, it then replaces the previous number in minNum. minNum is then
		// returned wherever findMin was originally called.
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] < minNum)
				minNum = numbers[count];
		}
			
		return minNum;
	}
	
	// computePositiveSum computes the sum of all positive values within the array numbers
	public static double computePositiveSum(double[] numbers, int count)
	{
		double  positiveSum = 0;
		
		// The following for loop determines whether each number is positive or not. If it is positive,
		// then it is added into the variable positiveSum. The resulting value is the sum of ONLY the
		// positive numbers in the array, which is then returned.
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] > 0)
				positiveSum = positiveSum + numbers[count];
		}
		
		return positiveSum;
	}
	
	// countNegative counts the number of negative values within the array numbers
	public static int countNegative(double[] numbers, int count)
	{
		int negCount = 0;
		
		// The following for loop determines whether the current value in the array is negative. If
		// it is negative, negCount increases by 1. The resulting number is the amount of negative
		// numbers within the array.
		for (count = 0; count < numbers.length; count++)
		{
			if (numbers[count] < 0)
				negCount++;
		}
		
		return negCount;
	}
}



Failed Test Case (output4.txt):
4 2 0 1 3

It seems to return '0' as the lowest number, but the desired outcome is for the program to terminate when it encounters '0', and doesn't include it in the array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1