java.util.Scanner;
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Jesper Young wrote:Line 1 should have been:
You forgot the import keyword.
Vardan Negi wrote:I had tried using import but the error it throws now is 'undefined symbol' .. it is not able to recognize the object s.
Vardan Negi wrote:Methods like nextInt() , useDelimiter() are defined under Scanner class.
Jesper Young wrote:
Vardan Negi wrote:I had tried using import but the error it throws now is 'undefined symbol' .. it is not able to recognize the object s.
Vardan Negi wrote:Methods like nextInt() , useDelimiter() are defined under Scanner class.
It looks like you're answering your own question.
Those methods are indeed in class Scanner, but in line 9 you are trying to call useDelimiter() on a Demo object - not on a Scanner object. In fact, you aren't using class Scanner at all in your source code.
I don't know what you're expecting, but importing class Scanner does not somehow add the methods of class Scanner to your class Demo...
Let's have a closer look at your line 9:
What happens here?
You create a new Demo object, passing a string input to the constructor. That will go wrong, because your class Demo does not have a constructor that takes a string as an argument.
Suppose you'd have such a constructor, and the Demo object would be created. Then you're trying to call the method useDelimiter() on that new Demo object. But your class Demo doesn't contain a useDelimiter() method, so that won't work either.
Third, suppose there would be a working useDelimiter() method in class Demo, you're assigning the return value of that method to a variable s of type Demo.
Summary:
Your class Demo is missing a constructor that takes a String Your class Demo is missing a method useDelimiter() that returns a Demo object
But probably you meant to write something different than you did.
Vardan Negi wrote:Changing Demo to Scanner compiles. But still the class is not able able to locate the methods defined under Scanner after including 'import java.util.Scanner;' .
Jesper Young wrote:The methods in class Scanner can only be called on a Scanner object. This doesn't have anything to do with your path or classpath.
How exactly did you change your code and what is the exact error message that you get?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jesper Young wrote:Aha, so your compilation error is solved.
The error that you get now happens because the input string doesn't match what the scanner expects.
Look at your input string: "1 fish 2 fish red fish blue fish"
You specified the regular expression "\\s*red\\s*" to be used as the delimiter for scanning the input string. This regular expression means: zero or more spaces, followed by the letters "red", followed by zero or more spaces. So the input string will be split into two tokens:
"1 fish 2 fish"
"fish blue fish"
Now look at what you're doing in lines 10-13. In line 10, you're trying to parse the first token as an integer. But "1 fish 2 fish" isn't an integer, so you get an InputMismatchException.
Vardan Negi wrote:
Jesper Young wrote:Aha, so your compilation error is solved.
The error that you get now happens because the input string doesn't match what the scanner expects.
Look at your input string: "1 fish 2 fish red fish blue fish"
You specified the regular expression "\\s*red\\s*" to be used as the delimiter for scanning the input string. This regular expression means: zero or more spaces, followed by the letters "red", followed by zero or more spaces. So the input string will be split into two tokens:
"1 fish 2 fish"
"fish blue fish"
Now look at what you're doing in lines 10-13. In line 10, you're trying to parse the first token as an integer. But "1 fish 2 fish" isn't an integer, so you get an InputMismatchException.
I get your point.
http://www.dil.univ-mrs.fr/docs/j2sdk/1.5/api/java/util/Scanner.html
The code i posted is from the above link itself. Why would they post wrong codes then ?