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 stringinput 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
Greenhorn
Joined: Jul 12, 2010
Posts: 15
posted
0
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.
You are right indeed. 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;' . Should i make changes with the classpath or path ?
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?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
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;' .
I'm confused. If the code compiles, what do you mean by "not able to locate the methods"? If there are error messages, post them along with the altered code.
Vardan Negi
Greenhorn
Joined: Jul 12, 2010
Posts: 15
posted
0
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?
I replaced Demo with Scanner obviously not the class name.
code compiles.
And this is what comes on the screen after running the code.
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Demo.main(Demo.java:10)
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.
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.
The code i posted is from the above link itself. Why would they post wrong codes then ?
Vardan Negi
Greenhorn
Joined: Jul 12, 2010
Posts: 15
posted
0
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.
The code i posted is from the above link itself. Why would they post wrong codes then ?
Alright i have understood the mistake i was making. Firstly in my code the string to be delimited is 'red' . But in the link i gave above , it is 'fish' . Also the type mismatched because the delimiter was positioned wrongly. With 'fish' as delimiter the nextint() works fine.