Can someone identify the error in this code for me? The input line gives an error.

import java.util.*;

public class Time_Convert_Test 
{

    Scanner number = new Scanner ( System.in );

	public static void main(String[] args) 
	{
		int firstValue;
		
		System.out.print( "Please enter time in hours:" );
		firstValue = number.nextInt(); 
		

	}

}

Recommended Answers

All 2 Replies

Change the following line from:

Scanner number = new Scanner ( System.in );

To:

static Scanner number = new Scanner ( System.in );

OR better yet, just move it inside of the main method:

public static void main(String[] args)
{
     Scanner number = new Scanner ( System.in ); 
     int firstValue;

     System.out.print( "Please enter time in hours:" );
     firstValue = number.nextInt();
}

See my some of my posts in the following article on how to do error checking with scanner:
http://www.daniweb.com/forums/post998852.html#post998852

Also see the following article about using "static":
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

commented: Super helper. +1

Change the following line from:

Scanner number = new Scanner ( System.in );

To:

static Scanner number = new Scanner ( System.in );

OR better yet, just move it inside of the main method:

public static void main(String[] args)
{
     Scanner number = new Scanner ( System.in ); 
     int firstValue;

     System.out.print( "Please enter time in hours:" );
     firstValue = number.nextInt();
}

See my some of my posts in the following article on how to do error checking with scanner:
http://www.daniweb.com/forums/post998852.html#post998852

Also see the following article about using "static":
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Thanks much for your help.

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.