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.
Code Java:
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
Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors
Quote:
I keep getting errors
Please copy and paste here the full text of the error messages.
It'll help us help you.
Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors
Oh, sorry
Code Java:
--------------------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.
Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors
The import statements should be before any class definitions.
Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors
Quote:
Originally Posted by
Norm
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! :D
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?
Code Java:
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
Code Java:
--------------------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.
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!
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.
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.
Code Java:
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
Code Java:
--------------------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.
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?
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).
Code Java:
System.out.println( "The sum is +\nThe product is +nThe average is +", sum, product, average );
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:
Code java:
System.out.println("The sum is "+sum+"/nThe product is "+product+"/nThe average is " +average);
Re: I'm Pretty Sure I Got It Right Yet I Keep Getting Errors
That did the trick. Thank you so much man.
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.