Results 1 to 8 of 8
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default error code help please..

    Sweets.java:8: error: cannot find symbol
    Scanner inp=new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    Sweets.java:8: error: cannot find symbol
    Scanner inp=new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    2 errors


    this is my code..

    //determine the output of number sweets per child
    public class Sweets
    {
    //

    public static void main (String args[])
    {
    Scanner inp=new Scanner(System.in);
    int sweet = inp.nextInt();
    int pupil =inp.nextInt();
    int eachPupil = sweet/pupil;
    int leftForTeacher= sweet%pupil;
    System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");

    }

    }

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: error code help please..

    import java.util.Scanner

  3. #3
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code help please..

    sorry what does this mean and what i change this with

    many thanks

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: error code help please..

    You need to add that import statement at the beginning of your code.

  5. #5
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code help please..

    this is what i am getting now

    H:\FPT>javac Sweets.java
    Sweets.java:2: error: '{' expected
    public class Sweets
    ^
    1 error

    //determine the output of number sweets per child
    public class Sweets


    Scanner import java.util.Scanner;


    {
    //

    public static void main (String args[])

    {

    Scanner inp=new Scanner(System.in);
    int sweet = inp.nextInt();
    int pupil =inp.nextInt();
    int eachPupil = sweet/pupil;
    int leftForTeacher= sweet%pupil;
    System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");

    }

    }

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: error code help please..

    to clarify a little, java API includes many packages as for example java.util.Scanner. So if you want to use that package you can write your code like

    Java Code:
    ...
    java.util.Scanner sc = new java.util.Scanner(System.in);
    ...
    or to import Scanner in the beggining and use it later

    Java Code:
    import java.util.Scanner;
    ...
    
    Scanner sc = new Scanner(System.in);
    ...

    Import statement have to be placed before your other code i.e. before you declare your class.

    The one thing that must be placed before import statement(s) is
    Java Code:
    package
    statement.

    in your case it could looks like

    Java Code:
    package nameYourPackage;
    
    import java.util.Scanner;
    
    //determine the output of number sweets per child
    public class Sweets
    
    ...
    Scanner sc =  new Scanner(System.in);
    ...
    any clearer?

  7. #7
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code help please..

    thank you so much for your help but i am so lost with this code (i don't think i will ever get Java)

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: error code help please..

    piece of cake :-)
    But seriously, it is not so hard, just don't give up.

Similar Threads

  1. error in code
    By Akap in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 06:37 AM
  2. error in this code
    By gradiente99 in forum Eclipse
    Replies: 11
    Last Post: 07-20-2010, 05:58 PM
  3. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 02:16 AM
  4. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 08:10 PM
  5. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •