Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What is wrong with my code?

    Here's the assignment I was given:
    The government introduces a new program of educational savings accounts. It adds a 20% bonus of whatever you contributed to the account up to a maximum of a $400 for a $2000 annual deposit. Create a program called CollegeSavings that obtains an amount deposited by the user and outputs the balance in the account including the bonus from the government.

    So I set up this code

    PHP Code:
    import hsa.*;
    // The "CollegeSavings" class.
    public class CollegeSavings
    {
        public static 
    void main (String[] args)
        {
    double depositbonusoutput;
        
    Stdout.println("Please input annual deposit up to $2000 annual deposit");
        
    deposit=Stdin.readDouble();
        if (
    deposit>20000bonus>400)
        
    Stdout.println("Unacceptable entry");
        else if (
    deposit<=2000bonus<=400)
        
    output=(deposit*1.20);
        
    Stdout.println("College savings grant:" "$" output);}
            
    // Place your code here
        
    // main method
    // CollegeSavings class 
    I know this is ridden with flaws but I just need to know where did I go wrong. If anyone was kind enough a revised code wouldn't hurt.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    There are many ways for code to go wrong. Can you explain what the problem is?
    Are there error messages? Please copy the messages and paste them here.
    The program's output is wrong. Please copy the program's output and paste it here and explain what is wrong and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Can you copy the error messages and paste them here? Images are hard to copy from for comments.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    import hsa.*;
    // The "CollegeSavings" class.
    public class CollegeSavings
    {
    public static void main (String[] args)
    {double deposit, bonus, output;
    Stdout.println("Please input annual deposit up to $2000 annual deposit");
    deposit=Stdin.readDouble();
    if (deposit>20000, bonus>400)
    Stdout.println("Unacceptable entry");
    else if (deposit<=2000, bonus<=400)
    output=(deposit*1.20);
    Stdout.println("College savings grant:" + "$" + output);}
    // Place your code here
    } // main method
    } // CollegeSavings class

    Here you go
    It says ". expected instead of this token", while highlighting the commas

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    What lines are the commas on?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    "if (deposit>20000, bonus>400) "
    and
    "else if (deposit<=2000, bonus<=400) "

    oh and the "}" in the "} // CollegeSavings class " is reported as error as well

    I wanted to make two if statement in the same row

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    What is the , inside the if statement supposed to do?
    How would you describe the condition you want to test in the if statement in English?

    See: http://docs.oracle.com/javase/tutori...bolts/op2.html


    What is the error message for the }?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    "What is the error message for the }?"
    It's clumped together in one error message, along with the other commas.

    I'm reading the link right now

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Hard to suggest how to fix an error when there is no descriptive message from the compiler.

    One suggestion is to check that all the {}s are properly paired.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    It says ". expected instead of this token" while highlighting two commas and a } sign

    Alright, thanks anyway

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    What is the , inside the if statement supposed to do?
    How would you describe the condition you want to test in the if statement in English?


    Check that all the {}s are properly paired.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    "How would you describe the condition you want to test in the if statement in English?"
    If the input is larger than 2000 and that the bonus is larger than 400, then shows "Unacceptable ..."
    Because I wanted the input to be less than or equal to 2000 and the problem states that the program only allows up to $2000 with a bonus up to $400, I thought putting , between these two if statement would bind them as one single statement.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    input is larger than 2000 and that the bonus is larger than 400,
    Then replace the , with the Conditional AND operator shown in the link I posted.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    I see

    Okay thank you, I will try that

Similar Threads

  1. What is wrong with my code?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 28th, 2013, 05:44 PM
  2. what is wrong with my code? please help
    By black.angel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 02:34 PM
  3. What is wrong with my code?
    By unleashed-my-freedom in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2012, 12:41 AM
  4. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 07:28 PM
  5. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM