aspose file tools
The moose likes Beginning Java and the fly likes error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "error" Watch "error" New topic
Author

error

Vardan Negi
Greenhorn

Joined: Jul 12, 2010
Posts: 15


Hi guys,

After compiling the above code i get the following error .. please help

Demo.java:1: class, interface, or enum expected
java.util.Scanner;
^
1 error

Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
What did you want to achieve with:


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Line 1 should have been:

You forgot the import keyword.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vardan Negi
Greenhorn

Joined: Jul 12, 2010
Posts: 15
Methods like nextInt() , useDelimiter() are defined under Scanner class.
Vardan Negi
Greenhorn

Joined: Jul 12, 2010
Posts: 15
Jesper Young wrote:Line 1 should have been:

You forgot the import keyword.


I had tried using import but the error it throws now is 'undefined symbol' .. it is not able to recognize the object s.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
I think you meant to write "new Scanner" instead of "new Demo".


Android appsImageJ pluginsJava web charts
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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
    Greenhorn

    Joined: Jul 12, 2010
    Posts: 15
    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 ?
    Jesper de Jong
    Java Cowboy
    Bartender

    Joined: Aug 16, 2005
    Posts: 12907
        
        3

    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
    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
    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)
    Jesper de Jong
    Java Cowboy
    Bartender

    Joined: Aug 16, 2005
    Posts: 12907
        
        3

    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.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19216

    Vardan Negi, please UseAMeaningfulSubjectLine next time. "error" is too generic.


    SCJP 1.4 - SCJP 6 - SCWCD 5
    How To Ask Questions How To Answer Questions
    Vardan Negi
    Greenhorn

    Joined: Jul 12, 2010
    Posts: 15
    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 ?

    Vardan Negi
    Greenhorn

    Joined: Jul 12, 2010
    Posts: 15
    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 ?



    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.
    Vardan Negi
    Greenhorn

    Joined: Jul 12, 2010
    Posts: 15
    Thankyou Jesper
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: error
     
    Similar Threads
    scanner question
    About regex
    Doubt regarding regex
    why doesnt this work?
    Scanner Doubt!