Skip to main content
gamedev.net
🔒 Locked

[java] Help porting some code

Started by Stompy9999 4 replies 1.1k views
Original Post
Stompy9999
Stompy9999
I have this bit of code I want to port from C++ to Java. The code loads in the position and type of a unit in a strategy game from a text file. The problem is, I'm not familiar enough with the Java file I/O system to do it. With C++ streams it was easy, but here I don't know. Okay, so to explain this, I have a text file containing information about the units in a strategy game. So I would have the data in the file arranged for one unit like this: 1 100 200 This would specify that there is a unit of type one at an x position of 100 and a y position of 200. In C++, I would load the data like so:

std::ifstream in("somedata.txt");
int type;
int x;
int y;
// Unit is a class containing the data for a strategy game unit
Unit someunit;

in >> type >> x >> y;

someunit.setType(type);
someunit.setX(x);
someunit.setY(y);

in.close();

But I'm not exactly sure how to do this in Java. I'm sorry if this is a simple concept, but I'm very new to Java. Also, I need to load in multiple units from one file, so any info on how to go to the next line would also be appreciated.
Son of Cain
Son of Cain
If we simply give you the code, it won't do you much good ;)

Take a look here, specially here.

Son Of Cain
a.k.a javabeats at yahoo.ca
H_o_p_s
H_o_p_s
Here is the tutorial I learned from for reading text files: TUTORIAL LINK.
BRING BACK THE BLACK (or at least something darker)
Stompy9999
Stompy9999
Quote:
Original post by Son of Cain
If we simply give you the code, it won't do you much good ;)

Take a look here, specially here.

Son Of Cain


Indeed, but I'm just trying to find out how to read in text files like the C++ streams. Thanks for all the help though.

EDIT: Just read the File I/O one there and it was exactly what I'm looking for. Thanks again.
Son of Cain
Son of Cain
No problem ;) Such a task in Java gets confusing because of the number of wrappers around these streams. That's why I pointed you to the standard tutorial for the API - it can guide you through the misunderstandings ;)

Good luck.

Son Of Cain
a.k.a javabeats at yahoo.ca
CaptainJester
CaptainJester
Sometimes it takes an example to see what is in the API. Especially when you are just starting out.

The two things you need to look at are how to read the file in line by line and then parse the line. BufferedReader can read lines in and StringTokenizer is your parsing tool. Try to figure it out by looking at the API for those 2 classes before looking at the code below.
import java.io.*;public class Test {    public static void main(String args[]) {        try {            String names[] = {"Character", "X Pos", "Y Pos"};            int x = 0;            BufferedReader in = new BufferedReader(new FileReader("test.txt"));            String line = null;            do {                line = in.readLine();                if(line != null) {                    x = 0;                    StringTokenizer st = new StringTokenizer(line, " ");                    while(st.hasMoreTokens()) {                        System.out.print(names[x] + " = " + st.nextToken() + " ");                        x++;                    }                    System.out.println();                }            } while(line != null);        }        catch(Exception e) {            e.printStackTrace();        }    }}

Test file test.txt.
1 100 2002 400 6003 50 704 800 40000
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.