6 Replies - 824 Views - Last Post: 13 October 2012 - 08:27 AM Rate Topic: -----

#1 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Reading a list of integers from a file

Posted 08 October 2012 - 05:31 PM

I'm trying to get a program that generates random points of a triangle to a file then to read from that file the points and use those points to find various other things. I'm having trouble getting it to read from the file. What I've tried is in the program right now but whenever I try to run it I get the error :

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)

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a list of integers from a file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Reading a list of integers from a file

Posted 08 October 2012 - 08:03 PM

Please post your error stack

x = Integer.parseInt(str.substring(1, str.length() - 1));
str = scan.next();
y = Integer.parseInt(str.substring(0, str.length() - 1));

can't see how you can have a -1 index for your substring()
Was This Post Helpful? 1
  • +
  • -

#3 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Reading a list of integers from a file

Posted 09 October 2012 - 08:26 AM

I forgot to include the scan.close(); when I copied the code over.

So for the substring() its pointless to have the -1?
Was This Post Helpful? 0
  • +
  • -

#4 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Reading a list of integers from a file

Posted 10 October 2012 - 06:04 PM

I'm sorry if I sound noobish or anything, I'm still trying to learn how to do some of this. But I've reworked my code to only output one set of points to hopefully make it easier. I'm still confused as to how to properly use the Interger.parseInt(str.substring());

package triangletesttype;
import java.io.*;
import java.util.*;
public class TriangleTestType {
    private static int x1, y1, x2, y2, x3, y3;
    private static String str;
    private static double sideA, sideB, sideC;
    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");
        x1 = randInt.nextInt(150);
        y1 = randInt.nextInt(150);
        do{ x2 = randInt.nextInt(150);
            y2 = randInt.nextInt(150);
        }while ((x1 == x2) && (y1 == y2));
        do{ x3 = randInt.nextInt(150);
            y3 = randInt.nextInt(150);
        }while (((x2 - x1) * (y3 - y1)) == ((y2 - y1) * (x3 - x1)));
        outFile.printf("(%3d, %3d) (%3d, %3d) (%3d, %3d) %n", x1, y1, x2, y2, x3, y3);
        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")).useDelimiter("( , )");
        str = scan.next(); x1 = Integer.parseInt(str.substring(1, 4));
        str = scan.next(); y1 = Integer.parseInt(str.substring(6, 9));
        str = scan.next(); x2 = Integer.parseInt(str.substring(12, 15));
        str = scan.next(); y2 = Integer.parseInt(str.substring(17, 20));
        str = scan.next(); x3 = Integer.parseInt(str.substring(23, 26));
        str = scan.next(); y3 = Integer.parseInt(str.substring(28, 31));
        scan.close();
        Point a = new Point(x1, y1); Point b = new Point(x2, y2); Point c = new Point(x3, y3);
        sideA = (y2 - y1) / (x2 - x1);
        sideB = (y3 - y2) / (x3 - x1);
        sideC = (y3 - y1) / (x3 - x1);
        System.out.println("Side a: " +a+ " Side b: " +b+ " Side c: " +c+ TriangleType(a, b, c));
    }    
    public static void main(String[] args) throws FileNotFoundException {
        TriangleTestType.GenerateRandomTriangles(1);
        TriangleTestType test = new TriangleTestType();
        test.TestTriangle();
    }
}


the output is now just : ( 60, 148) ( 79, 47) ( 65, 53)

When I run it now I get this error :
Exception in thread "main" java.lang.NumberFormatException: For input string: " 60"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at triangletesttype.TriangleTestType.TestTriangle(TriangleTestType.java:42)
at triangletesttype.TriangleTestType.main(TriangleTestType.java:58)
Java Result: 1

From what it looks like I have it reading properly but its not converting correctly or at all unless I'm just completely not understanding it. I haven't been able to find much help about the situation from google or from my book.
Was This Post Helpful? 0
  • +
  • -

#5 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Reading a list of integers from a file

Posted 10 October 2012 - 06:16 PM

The number format is happening because of the space. You are substringing to allow for spaces. The easiest (not really the most efficient) would be to just use the String method trim() (which gets rid of any white space) on every substring. Otherwise find a way to get rid of spaces when you substring.
Was This Post Helpful? 1
  • +
  • -

#6 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Reading a list of integers from a file

Posted 10 October 2012 - 06:30 PM

That solved my problem, Thank you so very very much!!!
Was This Post Helpful? 0
  • +
  • -

#7 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Reading a list of integers from a file

Posted 13 October 2012 - 08:27 AM

Sorry to be bothering again but I have another question. If i wanted to read multiple lines from the file how would i go about doing that? I tried using a while(scan.hasNextLine()) and a while(scan.hasNext()) but with each one when I run it only reads the one line and that's it. I then added a scan.nextLine() into the while after the print statement but I get the error :

Point a: (60, 148) Point b: (79, 47) Point c: (65, 53) Triangle is Scalar.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at testtriangletype.TestTriangleType.TestTriangle(TestTriangleType.java:68)
at testtriangletype.TestTriangleType.main(TestTriangleType.java:76)
Java Result: 1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1