Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: New to Java need help in class

  1. #1
    piglet972 is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default New to Java need help in class

    I have to do the following
    2) Prompt for and accept 3 integers from the keyboard.
    3) Add the following calculation to the java
    program. The calculation must be written
    using java code.
    add int number1 to int number3 plus 5
    then multiply the results by 2
    then multiply by int number2
    4) Display the results on a seperate line saying "The answer is "

    This is what I have...any advice would be appreciated

    public class Prog1 {

    public static void main ( string args [] )
    {
    Scanner input = new Scanner ( System.in );

    int number1;
    int number3;

    System.out.print ( "Enter first integer:" );
    number1 = input.nextInt();

    System.out.print ( "Enter third integer:");
    number2 = input.nextInt();

    result = ((number1 + number3 + 5)*2)*number2

    System.outprintf( "The answer is %d\n");
    }
    }

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: New to Java need help in class

    What is your question exactly?

    As for the advice:
    Variable 'number2' has never been declared, just like 'result'.
    System.out.printf() is missing a parameter.
    You only ask the user for two integers.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: New to Java need help in class

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: New to Java need help in class

    Quote Originally Posted by JavaAdviser View Post
    System.out.printf() is missing a parameter.
    Missing a dot too

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: New to Java need help in class

    result = ((number1 + number3 + 5)*2)*number2
    is missing a semi-colon too.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: New to Java need help in class

    This thread is missing the OP too

    db
    PhQ likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    piglet972 is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: New to Java need help in class

    Quote Originally Posted by JavaAdviser View Post
    What is your question exactly?

    As for the advice:
    Variable 'number2' has never been declared, just like 'result'.
    System.out.printf() is missing a parameter.
    You only ask the user for two integers.

    How about now.....

    public class Prog1
    {

    public static void main ( string args [] )
    {
    Scanner input = new Scanner ( System.in );

    int x; // first number input by user
    int y; // second number input by user
    int z; // third number input by user
    int result; // answer of numbers

    System.out.print ( "Enter first integer:" )
    x = input.nextInt(); // read first integer

    System.out.print ( "Enter second integer:" )
    y = input.nextInt(); // read second integer

    System.out.print ( "Enter third integer:" )
    z = input.nextInt(); // read third integer

    result = ((x + z + 5)*2)*y; // calculate numbers

    System.out.printf( "The answer is %d\n", result );
    } // end method
    }

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: New to Java need help in class

    Your System.out.prints are missing a semi-colon too.
    Use [code ][/code ] tags to show your code. It makes it easier to see.
    Last edited by PhQ; 09-20-2012 at 04:16 PM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: New to Java need help in class

    Quote Originally Posted by PhQ View Post
    Use [code ][/code ] tags to show your code. It makes it easier to see.
    A link to the BBCode guide re: code tags was given at #3 in this thread.

    @piglet972: If you ignore links posted in responses, why should members here try to help at all? Also note that use of code tags is not optional. If you continue to ignore advice, the thread may be locked and you may be banned for a period.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 6
    Last Post: 03-01-2012, 03:07 PM
  2. Replies: 3
    Last Post: 01-26-2012, 11:20 PM
  3. Replies: 1
    Last Post: 05-31-2009, 10:57 PM
  4. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  5. class.java to class.exe
    By f_the_cook in forum New To Java
    Replies: 11
    Last Post: 07-16-2008, 02:24 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
  •