Java programming help

The java interpreter keeps on spitting out this error whenever I access Scanner.nextDouble()
NumEvOdd.java:10: error: non-static method nextDouble() cannot be referenced from a static context
double num = Scanner.nextDouble();

Please help

you need to call nextDouble() on an instance of scanner not the class itself

2 Likes

You are trying to call the method on the type instead of an instance.

There is no such thing :wink:
Well, nobody uses them anymore anyway.

Just correct my code

DecimalFormat df = new DecimalFormat("#.0");
Scanner input = new Scanner(System.in);
System.out.println("Put in your number");
double num = Scanner.nextDouble();
double numHalf = num/2;
switch (df.format(numHalf))
{
    case 0:
	System.out.println("Your number is even");
    	break;
    default:
	System.out.println("Your number is odd");
	break;

How do you expect to learn when you let others correct your code?

The problem is really simple: You are creating an instance of the scanner, just to then ignore it. Call nextDouble on the instance, not the type.

You know what, you are right, I am being lazy and not really learning by copping out and have someone else see the obvious, thank you

edit: Jesus christ it was so simple, thanks man

DecimalFormat df = new DecimalFormat("#.0");
// input is an instance of class Scanner
Scanner input = new Scanner(System.in);
System.out.println("Put in your number");
// double num = Scanner.nextDouble();
// This is wrong, .nextDouble() is a method for a Scanner Object,
// this needs to be our input object we created above
double num = input.nextDouble();
double numHalf = num/2;
switch (df.format(numHalf))
{
    case 0:
	System.out.println("Your number is even");
    	break;
    default:
	System.out.println("Your number is odd");
	break;
...