Looping through different sets

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 3
Reputation: slimmit is an unknown quantity at this point 
Solved Threads: 0
slimmit slimmit is offline Offline
Newbie Poster

Looping through different sets

 
0
  #1
Apr 23rd, 2009
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:

  1. public static XYPoint [] readCoordinates(String fileName){
  2. XYPoint points [] = null;
  3. BufferedReader r;
  4. double nPoints; //# of points
  5. String nextline;
  6.  
  7. try{
  8. InputStream is = new FileInputStream(fileName);
  9. r = new BufferedReader(new InputStreamReader(is));
  10. }
  11. catch (IOException e){
  12. System.out.println("IOException");
  13. return points;
  14. }
  15. //get # of points
  16. try{
  17. nextline = r.readLine();
  18.  
  19. if (nextline == null){
  20. System.out.println("Error");
  21. return points;
  22. }
  23.  
  24. nPoints = parseInteger(nextline);
  25.  
  26.  
  27. }
  28. catch (IOException e) {
  29. System.out.println("IOException" +
  30. fileName + "\n" + e);
  31. return points;
  32. }
  33.  
  34. points = new XYPoint [(int) nPoints];
  35.  
  36. //read in points
  37. for (int j = 0; j < nPoints; j++){
  38. try {
  39. nextline = r.readLine();
  40.  
  41. if (nextline == null){
  42.  
  43. System.out.println("Error");
  44. return points;
  45. }
  46.  
  47. points[j] = parsePoint(nextline); //points here
  48.  
  49. } catch (IOException e) {
  50. System.out.println("IOException");
  51. return points;
  52. }
  53. }
  54.  
  55. return points;
  56. }
  57. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Looping through different sets

 
0
  #2
Apr 23rd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: slimmit is an unknown quantity at this point 
Solved Threads: 0
slimmit slimmit is offline Offline
Newbie Poster

Re: Looping through different sets

 
0
  #3
Apr 24th, 2009
Originally Posted by VernonDozier View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Looping through different sets

 
0
  #4
Apr 24th, 2009
Originally Posted by slimmit View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 985
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Looping through different sets

 
0
  #5
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Looping through different sets

 
0
  #6
Apr 24th, 2009
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: slimmit is an unknown quantity at this point 
Solved Threads: 0
slimmit slimmit is offline Offline
Newbie Poster

Re: Looping through different sets

 
0
  #7
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC