I'm trying to figure the topic out. To begin, lets say I have different sets of points in a file with the following format:

2 //1st number represents number of points within set
3, 4 //following lines are points
5, 6
3
3, 4
5, 6
6, 2
..etc

I have figured out to perform a function on the first set, but I'm having a hard time figuring out how to loop a function with following sets.

Any help would be appreciated.

Here is my method for getting a set:

public static XYPoint [] readCoordinates(String fileName){
        XYPoint points [] = null;
        BufferedReader r;
        double nPoints;                          //# of points
        String nextline;
        
        try{
            InputStream is = new FileInputStream(fileName);
            r = new BufferedReader(new InputStreamReader(is));
        }
        catch (IOException e){
            System.out.println("IOException");
            return points;
        }
       //get # of points
        try{
        	nextline = r.readLine();

        	if (nextline == null){
        		System.out.println("Error");
        		return points;
        	}

        	nPoints = parseInteger(nextline);
        	
        	
        }
        catch (IOException e) {
            System.out.println("IOException" +
                               fileName + "\n" + e);
            return points;
        }
	    
        points = new XYPoint [(int) nPoints];  
        
        //read in points
        for (int j = 0; j < nPoints; j++){            
        	try {
        		nextline = r.readLine();
        		
        		if (nextline == null){
        			
				    System.out.println("Error");
				    return points;
        		}
		    
        		points[j] = parsePoint(nextline); //points here	
		    
        	} catch (IOException e) {
        		System.out.println("IOException");
        		return points;
        	}
        }
       
	 return points;
    }
}

Recommended Answers

All 6 Replies

Is the question how to read in all of the sets or how to do something with them once they are read in? If the question is how to read everything from the file, what calls this function? It appears that this function will only read in a single set. Is that the problem? Is it supposed to read in a single set or is it supposed to read in the entire file?

Is the question how to read in all of the sets or how to do something with them once they are read in? If the question is how to read everything from the file, what calls this function? It appears that this function will only read in a single set. Is that the problem? Is it supposed to read in a single set or is it supposed to read in the entire file?

it's suppose to:

read in a single set
perform function on set
read in next set
perform function on set
etc..

I am having problems implementing a loop to read in following sets.

it's suppose to:

read in a single set
perform function on set
read in next set
perform function on set
etc..

I am having problems implementing a loop to read in following sets.

OK, that's the overall goal of the program. What is the goal of the function you posted? In particular, it has a return value:

public static XYPoint [] readCoordinates(String fileName)

What does the function return? A single set? Or all the sets?

You need one more loop, to include your existing loops. This is the loop that will keep reading new sets until the file is empty. You can make it an infinite loop (while (true)...), and break or return out of it when the readline() hits E.O.F.

Also, consider moving the `parse' method to a separate implementation class coded to an interface to allow for greater flexibility.

Is that file format predefined or something which you have designed yourself? If the latter, then there are better ways of representing the same information. Just move all the point coordinates on a single line removing the comma in between them. Read a single line from the file and split it with `space character' as a delimiter. If the size of the array after split is not divisible by 2 [the number of coordinates which constitute a point in 2D], consider it to be an invalid set and skip the computation. If the size is divisible by 2, then the size of the point array should be the size of the split array divided by two.

Thank you to everyone. Thanks especially to James. I managed to solve my problem. All I needed was another for loop around the for loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.