| | |
Looping through different sets
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
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:
java Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
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?
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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?
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
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.
public static XYPoint [] readCoordinates(String fileName)What does the function return? A single set? Or all the sets?
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.
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.
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- Winsock Multi-Client Servers (C++)
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- get sets of info from db depending on hidden field's value (PHP)
- Please help with the assembly code (Assembly)
- Tutorial: Search a Database (ASP)
- Changing class name in onMouseOver (HTML and CSS)
- looping and spaces (C)
- using variable for dynamic from elements (JavaScript / DHTML / AJAX)
- Can you add pictures/sounds in a win32 console app? (C)
Other Threads in the Java Forum
- Previous Thread: Gregorian Calendar program
- Next Thread: Coin Purse Program
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows






