|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Swing - Need help reading and converting an array
Hey i don't know if anyone can help me. This is my first post so i don't know if you are supposed to post stuff like this without any code. I am writing part of the code for a car racing game. The problem is that i have to write a method that allows me to open "track" files to race on. The method needs to follow the instructions below
Each line in a track file has the format n*s where s is a shearOffset, and n is the number of sequential track segments that have the same shearOffset of s eg: 2*0.0 1*-1.0 2*0.0 - Use the Util.readFile() method to read the lines of the file into an array. For each line in the file: - Use a string tokenizer to split the line into the two strings - Convert the first string to an int, and the second string to a double. - Add an apropriate number of shear offsets to a ShearOffsetsBuilder using the data from this line. - After all lines are read, return an array containing all of the shear offsets added to your ShearOffsetsBuilder. This is what the ShearOffsetBuilder looks like. private class ShearOffsetsBuilder { private List shearOffsetList; public ShearOffsetsBuilder() { shearOffsetList = new ArrayList(); } public void addShearOffset(double shearOffset) { shearOffsetList.add(new Double(shearOffset)); } public double[] buildShearOffsets() { double[] shearOffsets = new double[shearOffsetList.size()]; for (int i = 0; i < shearOffsets.length; i++) shearOffsets[i] = ((Double)shearOffsetList.get(i)).doubleValue(); return shearOffsets; } } This is the Util.readFile method: public static String[] readFile(File file) { try { List lineList = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null) lineList.add(line); in.close(); String[] lineArray = new String[lineList.size()]; for (int i = 0; i < lineArray.length; i++) lineArray[i] = (String)lineList.get(i); return lineArray; } catch (IOException e) { return null; } } Any help would be much appreciated |
|
#2
|
|||
|
|||
|
PS I know i am supposed to show what i have written so it can be seen what is wrong. This is what i have written so far. I'm fairly certain it is horribly wrong but be nice. I am very very new at this
private double[] loadShearOffsetsFromFile(File file) { String[] linesArray = Util.readFile(file); if (linesArray == null) { return null; } else { for (int i = 0; i < linesArray.length; i++) { StringTokenizer st = new StringTokenizer(i); String n = st.nextToken(); String s = st.nextToken(); int ni = Integer.parseInt(n); int si = Integer.parseInt(s); int nisi = ni*si; ShearOffsetsBuilder build = new ShearOffsetsBuilder(); build.addShearOffset(nisi); } return null; } } |
|
#3
|
|||
|
|||
|
Welcome,
There's nothing wrong with posting code, in fact there are even special code tags to use. Check out these links for more info: As for your question, well, i didn't really see a question. Are you having an issue with something?
__________________
Open for extension, closed for modification |
|
#4
|
|||
|
|||
|
Sorry i guess i didn't make it very clear.
At the beginning of the first post i wrote is what i guess you could call pseudo code what i am supposed to be writing. The second post is what i have written so far which i know is completely wrong. I really just don't have much of an idea how to start this method or particularly what to write in the foor loop for the string tokenizer. cheers |
|
#5
|
||||
|
||||
|
Quote:
![]()
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#6
|
|||
|
|||
|
oh ok sorry guys. ok i will try this again.
This is my mission should i choose to accept it and thus yours too if you are willing to spend the time: Each line in a track file has the format n*s where s is a shearOffset, and n is the number of sequential track segments that have the same shearOffset of s eg: 2*0.0 1*-1.0 2*0.0 - Use the Util.readFile() method to read the lines of the file into an array. For each line in the file: - Use a string tokenizer to split the line into the two strings - Convert the first string to an int, and the second string to a double. - Add an apropriate number of shear offsets to a ShearOffsetsBuilder using the data from this line. - After all lines are read, return an array containing all of the shear offsets added to your ShearOffsetsBuilder. This is the Util.readFile() method: PHP Code:
The ShearOffsetsBuilder looks like this: PHP Code:
This is my code that i sort of gave up on out of frustration and just general lack of knowledge: PHP Code:
Now i understand you guys probably won't want to write the entire thing and i totall understand that. I was just hoping someone might just be able to put me on track. I am especially really confused as to what i need to send the string tokenizer in the first place Thanks for your time |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Swing - Need help reading and converting an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|