icecreeper01 icecreeper01 - 1 month ago 6
Java Question

Triangle types program with infile

So i was writing a program for my computer science class in java and i have no errors but it won't run. I have to do it with an in file also.
The project i am trying to do is here
http://www.doralacademyprep.org/ourpages/auto/2016/10/28/50508547/C1%20PRG%20JR.pdf

the error message is here

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at GeeImATree.main(GeeImATree.java:58)


Here is the code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class GeeImATree {

public static boolean isEquilateral(double sideA, double sideB, double sideC) {
if(sideA == sideB && sideB == sideC) {
return true;
}
return false;
}
public static boolean isIsosceles(double sideA, double sideB, double sideC) {
if(!isEquilateral(sideA,sideB,sideC)){
return (sideA == sideB || sideB == sideC || sideA == sideC);
}
return false;
}
public static boolean isRight(double sideA, double sideB, double sideC) {
if(!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC)){
return ( (sideA*sideA) == (sideB*sideB)+(sideC*sideC) || (sideB*sideB) == (sideA*sideA)+(sideC*sideC) || (sideC*sideC) == (sideA*sideA)+(sideB*sideB));
}
return false;
}
public static boolean isObtuse(double sideA, double sideB, double sideC) {
if (!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC) && !isRight(sideA,sideB,sideC)) {
return ( (sideA*sideA) > (sideB*sideB)+(sideC*sideC) || (sideB*sideB) > (sideA*sideA) + (sideC*sideC) || (sideC*sideC) > (sideA*sideA)+(sideB*sideB) );
}
return false;
}
public static boolean isAcute(double sideA, double b, double c) {
if(!isEquilateral(sideA,b,c) && !isIsosceles(sideA,b,c) && !isRight(sideA,b,c) && !isObtuse(sideA,b,c)){
return( (sideA*sideA) < (b*b)+(c*c) || (b*b) < (sideA*sideA) + (c*c) || (c*c) < (sideA*sideA)+(b*b) );
}
return false;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

File infile = new File("GEOMETRY.IN");
Scanner scan = new Scanner(infile);
double x1, y1, x2, y2, x3, y3;
while(scan.hasNext())
{
x1 = scan.nextInt();
y1 = scan.nextInt();
x2 = scan.nextInt();
y2 = scan.nextInt();
x3 = scan.nextInt();
y3 = scan.nextInt();

double sideA = Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) );
double sideB = Math.sqrt( Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2) );
double sideC = Math.sqrt( Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2) );

System.out.println("a= "+sideA+", b= "+sideB+", c= "+sideC);
if (isEquilateral(sideA,sideB,sideC))
System.out.println("Equilateral");
if (isIsosceles(sideA,sideB,sideC))
System.out.println("Isosceles");
if (isRight(sideA,sideB,sideC))
System.out.println("Right");
if (isObtuse(sideA,sideB,sideC))
System.out.println("Obtuse");
if (isAcute(sideA,sideB,sideC))
System.out.println("Acute little triangle");
System.out.println("/n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");
}
scan.close();
}

}


In the Geometry.IN file is the following

0 0 3.5 1.2 9.85 2
0 0 5 0 5 9
0 0 6 8 8 0
0 0 3 7 1 7
0 0 8 4 8 -4

Answer

You should use nextDouble():

x1 = scan.nextDouble();
y1 = scan.nextDouble();
x2 = scan.nextDouble();
y2 = scan.nextDouble();
x3 = scan.nextDouble();
y3 = scan.nextDouble();

and try with this file:

0 0 3,5 1,2 9,85 2
0 0 5 0 5 9
0 0 6 8 8 0
0 0 3 7 1 7
0 0 8 4 8 -4

Moreover you have to round multiplication results, for example:

public static boolean isRight(double sideA, double sideB, double sideC) {
    if(!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC)){
        return ( Math.round(sideA*sideA) == Math.round(sideB*sideB)+Math.round(sideC*sideC) || Math.round(sideB*sideB) == Math.round(sideA*sideA)+Math.round(sideC*sideC) || Math.round(sideC*sideC) == Math.round(sideA*sideA)+Math.round(sideB*sideB));
    }
    return false;