I am trying to implement if slope one is positive(greater than zero) and slope1 is positive multiply by -1
‘Exception in thread “main” java.lang.Error: Unresolved compilation problems:
Syntax error on token “;”, . expected
slope1 cannot be resolved or is not a field
at LinearSlopeFinder.main(LinearSlopeFinder.java:25)
;
i have tried using an “,” instead but no dice
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
double x1, y1, x2, y2, n1, equation, constant = 0 ;
double slope, slope1, slopeAns;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? example: x,y ... ");
String coordinate1 = myScanner.nextLine();
String coordinates[] = coordinate1.split(",");
x1 = Integer.parseInt(coordinates[0]);
y1 = Integer.parseInt(coordinates[1]);
System.out.print(" What is the second set of cordinants? example: x,y ... ");
String coordinate2 = myScanner.nextLine();
String coordinates1[] = coordinate2.split(",");
x2 = Integer.parseInt(coordinates1[0]);
y2 = Integer.parseInt(coordinates1[1]);
//remember it is Rise over Run Y's over X's
slope = (y1-y2);
slope1= (x1-x2);
slopeAns= slope / slope1 ;
//below is the part that is not compiling but I am trying to insert
if ( slope > 0 ; slope1 > 0 ){
slope = slope * -1;
slope1 = slope1 * -1;
}
In Java you are looking for an AND operator for the if statement to combine the two Boolean results from slope > 0 and slope1 > 0 into one Boolean. The AND operator is && so try:
Other Boolean logical operators are | (OR), & (AND), ^ (XOR), ! (NOT), || (short-circuit OR), && (short-circuit AND), == (EQUAL TO), != (NOT EQUAL TO), ?: (IF-THEN-ELSE).
The difference between | and || is that in Java if the first statement turns out to be true then it will not evaluate the second or more statement with || but it will with |. The same goes for && if the first statement is equal to false.