• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

error

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you want to achieve with:

java.util.Scanner;

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 should have been:

You forgot the import keyword.
 
Vardan Negi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods like nextInt() , useDelimiter() are defined under Scanner class.
 
Vardan Negi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant to write "new Scanner" instead of "new Demo".
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Sheriff
    Posts: 22849
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vardan Negi, please UseAMeaningfulSubjectLine next time. "error" is too generic.
     
    Vardan Negi
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thankyou Jesper
     
    reply
      Bookmark Topic Watch Topic
    • New Topic