954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can't find Scanner class?

I'm not quite sure what is wrong, and I am VERY new to programming but heres teh problem:

Scanner scanner;
scanner = new Scanner();

This is what I have, and this is the error message:

cannot find symbol
symbol: constructor Scanner()
location: class.java.util.Scanner


And also, there is a problem with another one of my codes that I'm not sure about...:

if(guess == secretWord)

error message:
Comparing String using =! or ==

not too sure about this one...anyways if anyone can help I would appreciate it.

phintastic
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

for using the Scanner class, don't you need to make an import of the class?

import java.util.Scanner;


concerning your second question: the == and != are indeed not the right way to check whether two Strings are equal or not.

a String is an Object, and should be compared using the method equals() (or, equalsIgnoreCase())

for instance:

String guess = "guess";
String secret = "secret";

boolean test = guess.equals(secret);
// test = false;
if(!guess.equals(secret))
  System.out.println("this line will be printed, since the two Strings are not equal");
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

The problem wasn't the import. It looked like the OP already had the import listed.

The problem is that the Scanner class does not provide a "default" constructor for Scanner objects.

For a list of proper Constructors for Scanner objects, refer to this link .

The most common use for a Scanner object is the provide a way for pulling specific information out of a File or InputStream.

Example--

import java.util.Scanner;

public class MyScannerTest{

	public static void main(String... args){

		Scanner kb = new Scanner(System.in);

		System.out.println("Enter something, please... anything!\n");
		System.out.println("\nYou entered: " + kb.next());
	}
}


--though I think BufferedReader does the job much better than Scanner. Then again, each class solves a different problem in a similar way.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You