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

    Default sweets Java program

    hi

    i am still trying to work out how to do this program i have had lots of help on here on this and for that i am so grateful i am finding java somewhat confusing to say the least but i am not going to give up.

    scenario (just in case you don't know all ready.

    A teacher has bought a packet of 40 sweets which she is going to share out between her class of 14 students.

    Write a program to determine and output the number of sweets each child will receive. As a single sweet can not be shared between pupils, the program must calculate the number of sweets per child and the number that the teacher keeps for herself.

    my code so far (with help on here and tutorials)


    import java.until.*;
    //determine the ouput of number of sweets per child
    public class Sweets

    {

    public static void main (String args[])

    {

    //create Scanner to read in data
    Scanner myKeyboard = new Scanner(System.in);


    //prompt user for input use print to leave cursor on line

    System.out.print("Number of sweets ");
    int sweets = myKeyboard.nextInt();


    System.out.print(" pupil ");
    int pupil = myKeyboard.nextInt();

    System.out.print("Each pupil ");
    int sweet/pupil = myKeyboard.nextInt();


    System.out.print("Left for teacher ");
    int sweet%pupil= myKeyboard.nextInt();


    System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");

    }

    }


    the 2 errors i am getting still.

    H:\FPT>javac Sweets.java
    Sweets.java:25: error: ';' expected
    int sweet/pupil = myKeyboard.nextInt();
    ^
    Sweets.java:29: error: ';' expected
    int sweet%pupil= myKeyboard.nextInt();
    ^
    2 errors

    Many thanks..

  2. #2
    AndyVidk is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: sweets Java program

    Hi there.
    You cannot declare variable names with % and /

    Java Code:
    Sweets.java:25: error: ';' expected
    int sweet/pupil = myKeyboard.nextInt();
    ^
    Sweets.java:29: error: ';' expected
    int sweet%pupil= myKeyboard.nextInt();
    But this should be a solution.


    Java Code:
    System.out.print(pupil/sweets  + " for each pupil ");
    System.out.print(pupil%sweets + " Left for teacher ");

    Regards Andy.
    Last edited by AndyVidk; 10-09-2011 at 11:40 AM.

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

    Default Re: sweets Java program

    hi thanks for the speedy reply so do i need to rewrite my code then?

    Many thanks Andy

  4. #4
    AndyVidk is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: sweets Java program

    Look at my above code, i was a bit to slow to add it in the EDIT. so you probably haven't seen it.

    Regards Andy.

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

    Default Re: sweets Java program

    code:

    import java.until.*;
    //determine the ouput of number of sweets per child
    public class Sweets

    {

    public static void main (String args[])

    {

    //create Scanner to read in data
    Scanner myKeyboard = new Scanner(System.in);


    //prompt user for input – use print to leave cursor on line

    System.out.print("Number of sweets ");
    int sweets = myKeyboard.nextInt();

    System.out.print(" pupil ");
    int pupil = myKeyboard.nextInt();

    int System.out.print(pupil/sweets + " for each pupil ");

    int System.out.print(pupil%sweets + " Left for teacher ");



    System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");

    }

    }

    2 different errors now..

    H:\FPT>javac Sweets.java
    Sweets.java:19: error: ';' expected
    int System.out.print(pupil/sweets + " for each pupil ");
    ^
    Sweets.java:21: error: ';' expected
    int System.out.print(pupil%sweets + " Left for teacher ");

    many thanks Andy

    ps. how to make the code easy to read for you like you did me when you post.

  6. #6
    AndyVidk is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: sweets Java program

    I think you have misunderstood something :) you should rad up on the basics again. but here i think this will work:

    Java Code:
    import java.until.*;
    //determine the ouput of number of sweets per child
    public class Sweets
    
    {
    
    public static void main (String args[])
    
    {
    
    //create Scanner to read in data
    Scanner myKeyboard = new Scanner(System.in);
    
    
    //prompt user for input – use print to leave cursor on line
    
    System.out.print("Number of sweets ");
    int sweets = myKeyboard.nextInt();
    
    System.out.print(" pupil ");
    int pupil = myKeyboard.nextInt();
    
    int eachPupil =  sweets/pupil;
    
    int leftForTeacher = sweets%pupil;
    
    System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher + " sweets are left for teacher");
    
    }
    
    }

    Regards Andy.
    Last edited by AndyVidk; 10-09-2011 at 12:02 PM. Reason: switched "sweets" and "pupil" around.

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

    Default Re: sweets Java program

    many thanks i am sorry for being a pain i am just starting Java and finding it hard to grasp and is really confusing me but yes i will have to go over from the start again i think (i will get there)

    many thanks Andy..

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

    Default Re: sweets Java program

    H:\FPT>javac Sweets.java
    Sweets.java:1: error: package java.until does not exist
    import java.until.*;
    ^
    Sweets.java:12: error: cannot find symbol
    Scanner myKeyboard = new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    Sweets.java:12: error: cannot find symbol
    Scanner myKeyboard = new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    3 errors

    i still get these errors

  9. #9
    AndyVidk is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: sweets Java program

    Quote Originally Posted by andnlou2678 View Post
    many thanks i am sorry for being a pain i am just starting Java and finding it hard to grasp and is really confusing me but yes i will have to go over from the start again i think (i will get there)

    many thanks Andy..
    It was not meant as such, but the systom.out.print(String); is a void method meaning you it does not return anything than you can put in an int as such :
    Java Code:
    int someVariableName =system.out.println(String);
    If you want to save your result in a int you can do
    Java Code:
    int someVariableName =  aInt / anOtherInt;
    If you then want to print this you can say
    Java Code:
    System.out.println("Result: " + someVariableName);

    Quote Originally Posted by andnlou2678 View Post
    H:\FPT>javac Sweets.java
    Sweets.java:1: error: package java.until does not exist
    import java.until.*;
    ^
    Sweets.java:12: error: cannot find symbol
    Scanner myKeyboard = new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    Sweets.java:12: error: cannot find symbol
    Scanner myKeyboard = new Scanner(System.in);
    ^
    symbol: class Scanner
    location: class Sweets
    3 errors
    Try changing you import in the top to "util" instead of "until"

    Regards Andy.
    Last edited by AndyVidk; 10-09-2011 at 12:19 PM.

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

    Default Re: sweets Java program

    hi thks for your help got working but does this look right to you?
    H:\FPT>java Sweets
    Number of sweets 40
    Number of Students 14
    Number of sweets 0 Number of sweets per child14the number that the teacher keeps
    for herself

    Many thanks Andy..

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: sweets Java program

    Quote Originally Posted by andnlou2678 View Post
    hi thks for your help got working but does this look right to you?
    Nope, the first number should equal 2 (40/14) and the second number should be 12 (40%14). You must've used the wrong formulas ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •