I get an error on this line "System.out.println((num1/dem1)(num2/dem2)); "
The error says:
Multiple markers at this line - The left-hand side of an assignment must be a variable - Syntax error on token ")", AssignmentOperator expected after this token
package project;
import java.util.Scanner;
public class summerproject {
public static void main(String[] args)
{
Scanner in = new Scanner( System.in );
{
//INPUT NUMBERS
System.out.println("Enter Numerator 1. ");
int num1 = in.nextInt();
System.out.println("Enter Denominator 1. ");
int dem1 = in.nextInt();
System.out.println("Enter Numerator 2. ");
int num2 = in.nextInt();
System.out.println("Enter Denominator 2. ");
int den2 = in.nextInt();
}
System.out.println("Press 1 to multiply");
int mult = in.nextInt();
if (mult == 1)
{
System.out.println((num1/dem1)(num2/dem2));
}
}//ARGS BRACKET
}//END BRACKET
2 Answers 2
This line isn't valid:
System.out.println((num1/dem1)(num2/dem2));
(num1/dem1) and (num2/dem2) need to have an operator between them. For example, to multiply the two expressions together, use the * operator:
(num1/dem1)*(num2/dem2)
Java doesn't behave quite like mathematics, where concatenation implies multiplication. Instead, you have to explicitly multiply operands together.
In addition, you declared
int den2 = in.nextInt();
This should be
int dem2 = in.nextInt().
After adjusting braces appropriately, and making the above fixes, you should end up with:
public class summerproject {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//INPUT NUMBERS
System.out.println("Enter Numerator 1. ");
int num1 = in.nextInt();
System.out.println("Enter Denominator 1. ");
int dem1 = in.nextInt();
System.out.println("Enter Numerator 2. ");
int num2 = in.nextInt();
System.out.println("Enter Denominator 2. ");
int dem2 = in.nextInt();
System.out.println("Press 1 to multiply");
int mult = in.nextInt();
if (mult == 1) {
System.out.println((num1 / dem1) * (num2 / dem2));
}
}
}
An example run:
Enter Numerator 1.
20
Enter Denominator 1.
4
Enter Numerator 2.
30
Enter Denominator 2.
5
Press 1 to multiply
1
30
10 Comments
System.out is an instance of PrintStream, which has overloaded methods for printing the primitive types and objects. See docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html Thus, printing ints is just fine.You need an operator in the middle of your operations
Your code seems to indicate you want to multiply so try the code below instead...
System.out.println((num1/dem1)*(num2/dem2));
System.out.println((num1/dem1) + " " + (num2/dem2));or as mattingly pointed out, * between them to multiply.