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: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Yeah, I've been trying to debug this chunk of code to no avail. It seems correct yet I keep getting errors. I'm really new to this so bear with me.

    public class Arithmetic
    {
    import java.util.Scanner;
     
       public static void main( String args[] )
       {
          Scanner input = new Scanner( System.in );  
          int num2;
          int num3;
          int sum;
          int product;
          int average;
     
          System.out.println( "Enter first integer:" );
          number1 = input.nextInt();    
     
          System.out.println( "Enter second integer:" );
          number2 = input.nextInt();
     
          System.out.println( "Enter third integer:" );
          number3 = input.nextInt();
     
          sum = num1 + num2 + num3;
          product = num1 * num2 * num3;
          average = ( num1 + num2 + num3 ) / 3;
     
          System.out.println( "The sum is %d\nThe product is %d\nThe average is %d", sum, 
             product, average );
       }
    } // end class Arithmetic


  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: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    I keep getting errors
    Please copy and paste here the full text of the error messages.
    It'll help us help you.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    MuffinOfFun (September 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Oh, sorry

    --------------------Configuration: <Default>--------------------
    C:\Users\Cam\Desktop\Arithmetic.java:3: error: illegal start of type
    import java.util.Scanner;
    ^
    C:\Users\Cam\Desktop\Arithmetic.java:3: error: ';' expected
    import java.util.Scanner;
          ^
    C:\Users\Cam\Desktop\Arithmetic.java:3: error: illegal start of type
    import java.util.Scanner;
               ^
    C:\Users\Cam\Desktop\Arithmetic.java:3: error: ';' expected
    import java.util.Scanner;
                    ^
    C:\Users\Cam\Desktop\Arithmetic.java:3: error: <identifier> expected
    import java.util.Scanner;
                            ^
    5 errors
     
    Process completed.

  5. #4
    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: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    The import statements should be before any class definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Quote Originally Posted by Norm View Post
    The import statements should be before any class definitions.
    What?!
    Your norm is evolving!
    *Amazing norm transformation*
    Congratulations! You're Norm has transformed into a Super Moderator!

    ...
    Congratulations.

    Also, Norm gave you the correct answer. You import BEFORE the classes!

  7. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    So I fixed that and it's seemingly created a whole new slew of errors. Any more advice?
    import java.util.Scanner;
     
    public class Arithmetic
    {
          public static void main( String args[] )
       {
          Scanner input = new Scanner( System.in );  
          int num2;
          int num3;
          int sum;
          int product;
          int average;
     
          System.out.println( "Enter first integer:" );
          number1 = input.nextInt();    
     
          System.out.println( "Enter second integer:" );
          number2 = input.nextInt();
     
          System.out.println( "Enter third integer:" );
          number3 = input.nextInt();
     
          sum = num1 + num2 + num3;
          product = num1 * num2 * num3;
          average = ( num1 + num2 + num3 ) / 3;
     
          System.out.println( "The sum is %d\nThe product is %d\nThe average is %d", sum, product, average );
       }
    } // end class Arithmetic
    --------------------Configuration: <Default>--------------------
    C:\Users\Cam\Desktop\Arithmetic.java:15: error: cannot find symbol
          number1 = input.nextInt();    
          ^
      symbol:   variable number1
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:18: error: cannot find symbol
          number2 = input.nextInt();
          ^
      symbol:   variable number2
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:21: error: cannot find symbol
          number3 = input.nextInt();
          ^
      symbol:   variable number3
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:23: error: cannot find symbol
          sum = num1 + num2 + num3;
                ^
      symbol:   variable num1
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:24: error: cannot find symbol
          product = num1 * num2 * num3;
                    ^
      symbol:   variable num1
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:25: error: cannot find symbol
          average = ( num1 + num2 + num3 ) / 3;
                      ^
      symbol:   variable num1
      location: class Arithmetic
    C:\Users\Cam\Desktop\Arithmetic.java:27: error: no suitable method found for println(String,int,int,int)
          System.out.println( "The sum is %d\nThe product is %d\nThe average is %d", sum, product, average );
                    ^
        method PrintStream.println(Object) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(String) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(char[]) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(double) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(float) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(long) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(int) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(char) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(boolean) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println() is not applicable
          (actual and formal argument lists differ in length)
    7 errors
     
    Process completed.

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Hint: The cannot find symbol error is most usually caused by typos and/or trying to use variables you have not actually initialized!
    Last edited by ChicoTheMan94; September 26th, 2012 at 08:30 PM.

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    You're trying to use variables that you've not yet declared. The errors are telling you exactly which ones are the offending variables.

  10. The Following User Says Thank You to curmudgeon For This Useful Post:

    MuffinOfFun (September 26th, 2012)

  11. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Alright I'm slowly getting it. I think there's just one more thing left that I can't figure out.

    import java.util.Scanner;
     
    public class Arithmetic
    {
          public static void main( String args[] )
       {
          Scanner input = new Scanner( System.in );  
          int number1;
          int number2;
          int number3;
          int sum;
          int product;
          int average;
     
          System.out.println( "Enter first integer:" );
          number1 = input.nextInt();    
     
          System.out.println( "Enter second integer:" );
          number2 = input.nextInt();
     
          System.out.println( "Enter third integer:" );
          number3 = input.nextInt();
     
          sum = number1 + number2 + number3;
          product = number1 * number2 * number3;
          average = ( number1 + number2 + number3 ) / 3;
     
          System.out.println( "The sum is %d\nThe product is %d\nThe average is %d", sum, product, average );
       }
    } // end class Arithmetic

    --------------------Configuration: <Default>--------------------
    C:\Users\Cam\Desktop\Arithmetic.java:28: error: no suitable method found for println(String,int,int,int)
          System.out.println( "The sum is %d\nThe product is %d\nThe average is %d", sum, product, average );
                    ^
        method PrintStream.println(Object) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(String) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(char[]) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(double) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(float) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(long) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(int) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(char) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(boolean) is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println() is not applicable
          (actual and formal argument lists differ in length)
    1 error
     
    Process completed.

    Seriously though you guys are awesome.

  12. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    I've never used %d for output, have you tried using +'s? For example:

    System.out.println("The info here is "+variable for info+"and "+variable for more info); ect?

  13. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    I'm doing what you suggested like this and I still get the same error. Unless of course I'm doing it wrong (which I probably am).
     System.out.println( "The sum is +\nThe product is +nThe average is +", sum, product, average );

  14. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    Yeah, just a couple of syntax errors, it would be like this:
    System.out.println("The sum is "+sum+"/nThe product is "+product+"/nThe average is " +average);

  15. The Following User Says Thank You to ChicoTheMan94 For This Useful Post:

    MuffinOfFun (September 26th, 2012)

  16. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    That did the trick. Thank you so much man.

  17. #14
    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: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors

    The error message includes: method PrintStream.println(Object)
    That shows that the PrintStream class's printn() method arg list has ONE argument.
    See the API doc: Java Platform SE 7
    You are calling the println() method with more than one arg (args are separated by commas)

    There are methods that take an arbitrary number of args. See the printf() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: June 22nd, 2012, 05:17 PM
  2. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  3. Pretty basic java program..need some help!
    By LAerving in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 26th, 2012, 12:09 AM
  4. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  5. Pretty new to Java can someone help with an addObject() problem?
    By alan120184 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2009, 12:48 PM