Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 6th, 2008, 08:25 PM
saximus saximus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 saximus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 48 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old May 7th, 2008, 05:22 AM
saximus saximus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 saximus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 48 sec
Reputation Power: 0
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;


}
}

Reply With Quote
  #3  
Old May 8th, 2008, 09:47 PM
tfecw tfecw is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Nov 2004
Location: Washington DC
Posts: 2,207 tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tfecw User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 5 h 10 m 32 sec
Reputation Power: 427
Send a message via AIM to tfecw
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

Reply With Quote
  #4  
Old May 8th, 2008, 11:01 PM
saximus saximus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 saximus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 48 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old May 9th, 2008, 12:35 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,547 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109548 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 35 m 38 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
Originally Posted by saximus
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
You can start by modifying your post and wrapping your codes in java code tags. Lest for me it seems like I am looking at a black and white tv

Reply With Quote
  #6  
Old May 9th, 2008, 12:54 AM
saximus saximus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 saximus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 48 sec
Reputation Power: 0
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:
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 0lineArray.lengthi++)
                
lineArray[i] = (String)lineList.get(i);
            return 
lineArray;
        }
        catch (
IOException e)
        {
            return 
null;
        }
    } 


The ShearOffsetsBuilder looks like this:
PHP Code:
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 0shearOffsets.lengthi++)
                
shearOffsets[i] = ((Double)shearOffsetList.get(i)).doubleValue();
            return 
shearOffsets;
        }
    } 



This is my code that i sort of gave up on out of frustration and just general lack of knowledge:
PHP Code:
private double[] loadShearOffsetsFromFile(File file)
    {
        
String[] linesArray Util.readFile(file);
        if (
linesArray == null)
        {
            return 
null;
        }
        else
        {
            for (
int i 0linesArray.lengthi++)
            {
                
String lineString = new String(linesArray);
                
StringTokenizer st = new StringTokenizer(lineString);
                
String n st.nextToken();
                
String s st.nextToken();
                
int ni Integer.parseInt(n);
                
int si Integer.parseInt(s);

            }
            return 
null;
            
                
        }
    } 


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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Swing - Need help reading and converting an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway