Start New Discussion within our Software Development Community

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)))
                  break;
              else
                  lines.add(i, l);


        }
    }

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

}

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

if ( lines.contains(l))
  break;

instead.

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 article has been dead for over six months. Start a new discussion instead.