0

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(); 
		

	}

}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Teethous
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

Edited by cgeier: n/a

Votes + Comments
Super helper.
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.