I am trying to create Line objects and add them to an array list. The problem I am having is excluding any lines that are similar to each other. I have already created an equals method that compares two lines to determine if they are equal. I am having trouble using the while loop. I do not have an error message. It compiles just fine. It just will not read from the text file. I am stuck and do not know where else to go from here.

public void read( File fileName ) throws Exception
    reader = new Scanner(fileName);

    // Change to Arraylist. Make the name of the arraylist "lines" so that code in paintComponent works.
    //Have to read the first number before starting the loop
    int numLines = reader.nextInt();
    lines = new ArrayList <Line> (numLines);

    //This loop adds a new Line object to the lines array for every line in the file read.
    while( reader.hasNext() ) {
        for( int i = 0; i < numLines; i++ ) {
            int x = reader.nextInt();
            int y = reader.nextInt();
            Point beg = new Point(x,y);
            x = reader.nextInt();
            y = reader.nextInt();
            Point end = new Point(x,y);

            String color = reader.next();

              Line l =  new Line( beg, end, color );

              if (l.equals(lines.get(i)))
                  lines.add(i, l);


    //Print the action to the console
    System.out.println( "reading text file: " + fileName );

3 Years
Discussion Span
Last Post by stultuske

seems to me like your check isn't the way it should be.
try with:

if ( lines.contains(l))


my suggestion: break up your code.

method a: read the file into an arraylist
method b: iterate over the arraylist to check which ones to add.

or, just use a type that doesn't accept double values.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.