Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
What can I try to fix it or get it running better?
package testtriangletype;
import java.io.*;
import java.util.*;
public class TestTriangleType {
private static int x, y;
private static String str;
private class Point{
int x, y;
public Point(int x, int y){
this.x = x; this.y = y;}
public void Print(){ System.out.println(toString());}
public String toString(){
return("(" + x + ", " + y + ")");
}
}
public static void GenerateRandomTriangles(int numTriangles) throws FileNotFoundException{
Random randInt = new Random(0);
PrintWriter outFile = new PrintWriter("RandomTriangles.dat");
outFile.println("5 // numTriangles; Prints a list of the (x, y) of the vertices of a triangle.");
int i;
for (i = 0; i < numTriangles; i++){
outFile.printf("(%3d, %3d) (%3d, %3d) (%3d, %3d) %n", randInt.nextInt(150),
randInt.nextInt(150), randInt.nextInt(150), randInt.nextInt(150),
randInt.nextInt(150), randInt.nextInt(150));
}
outFile.close();
}
public String TriangleType(Point a, Point b, Point c){
if ((a == B)/> && (b == c)){
return "Triangle is Equilateral";
}
else if (((a == B)/> || (b == c)) && (a != c)){
return "Triangle is Isosceles";
}
else{ return "Triangle is Scalar";}
}
private void TestTriangle() throws FileNotFoundException{
Scanner scan = new Scanner(new File("RandomTriangles.dat"));
int numTri = scan.nextInt();
int i;
scan.nextLine();
ArrayList<Point> numSetOfPoints = new ArrayList<Point>();
for( i = 0; i < numTri; i++){
str = scan.next();
x = Integer.parseInt(str.substring(1, str.length() - 1));
str = scan.next();
y = Integer.parseInt(str.substring(0, str.length() - 1));
numSetOfPoints.add(new Point(x, y));
}
System.out.println(numSetOfPoints);
}
public static void main(String[] args) throws FileNotFoundException {
TestTriangleType.GenerateRandomTriangles(5);
TestTriangleType test = new TestTriangleType();
test.TestTriangle();
}
}
This is the output data:
5 // numTriangles; Prints a list of the (x, y) of the vertices of a triangle.
( 60, 148) ( 79, 47) ( 65, 53)
( 41, 111) ( 69, 104) ( 77, 77)
( 23, 112) ( 95, 44) ( 34, 75)
( 41, 20) ( 43, 138) ( 74, 47)
( 2, 10) ( 3, 82) ( 42, 23)

New Topic/Question
Reply



MultiQuote



|