I have a monopoly board that uses spaces. Each space needs to be initialized and some are different types of spaces like free parking which is a sub class of spaces.
So I have a board of 40 spaces: Space[] board = new Space[40];
I'd like all info like space name, and all the parameters needed to initialize a space to be included in a txt file which included the name, costs and other stuff needed or something like that.
13 replies to this topic
#1
Posted 11 May 2011 - 04:23 AM
|
|
|
#2
Posted 11 May 2011 - 05:14 AM
I used some code which I got off the internet to read a file. And I turned it into a static method. The method works in another class but the problem is that when I call that method, it reads the SAME line instead of the next line. How can I make it so that every time I call it, it read the next line in the system.
Quote
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {
public static String getInfo() throws IOException {
File file = new File("//LHS1371-FS1/Studentshd/FA003/Eula.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
// this statement reads the line from the file and print it to
// the console.
return(dis.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
return null;
}
}
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {
public static String getInfo() throws IOException {
File file = new File("//LHS1371-FS1/Studentshd/FA003/Eula.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
// this statement reads the line from the file and print it to
// the console.
return(dis.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
return null;
}
}
Quote
import java.io.IOException;
public class Board {
Space[] board = new Space[40];
//testing the file input here:
public static void main(String[] args) throws IOException {
String str;
for(int x = 0; x <3; x++){
str = FileInput.getInfo();
System.out.print(str);}
}
//End of test code block
}
public class Board {
Space[] board = new Space[40];
//testing the file input here:
public static void main(String[] args) throws IOException {
String str;
for(int x = 0; x <3; x++){
str = FileInput.getInfo();
System.out.print(str);}
}
//End of test code block
}
#3
Posted 11 May 2011 - 05:19 AM
That's perfectly standard. All you'd need to do is use BufferedReader wrapping a FileReader and you're good.
Space[] loadSpaces(String filename) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
ArrayList<Space> spaces = new ArrayList<>(); // JDK 7 diamond operator
for (String str; (str = reader.readLine()) != null;)
{
// Perform your initialization of the Space object here based on the
// data provided in str, placing them into the spaces ArrayList.
}
return spaces.toArray(new Space()); // Used new since it's unknown if there are any elements!
}
Wow I changed my sig!
#4
Posted 11 May 2011 - 01:04 PM
I tested the code while in school and some error came up with the array list. Wasn't able to post them cause I ran out of time. And I lost my flash drive with all of my data on it :crying:
I'm going to have to wait till tomorrow and show you guys.
I'm going to have to wait till tomorrow and show you guys.
#5
Posted 11 May 2011 - 02:51 PM
ArrayList<Space> spaces = new ArrayList<>(); // JDK 7 diamond operatorYou're probably receiving errors because JDK 7 isn't complete yet. (Thought it is nearing completion) JDK 7 allows you to leave the second set of <> empty.
You'll need to change it to:
ArrayList<Space> spaces = new ArrayList<Space>();
You can view more information about JDK 7 here:
JDK 7
#6
Posted 12 May 2011 - 07:27 AM
Thanks Lethal and Zeke.
But I kinda forgot about one thing. That is that my parameters for the space object take a double and a string. And the txt file reader only returns a string. I thought about making it so that each space has to lines, one for the str to be converted into str, and the other for the name of the space.
Here's the constructor for space:
Boardwalk
300.00 //to be converted
Next Space
Cost of next Space //covert double to fit in parameters for space constructor
-end of board-
But I kinda forgot about one thing. That is that my parameters for the space object take a double and a string. And the txt file reader only returns a string. I thought about making it so that each space has to lines, one for the str to be converted into str, and the other for the name of the space.
Here's the constructor for space:
public Space(double cost, String name){
price = cost;
spaceName = name;
}
public class Board {
Space[] board = new Space[40];
Space[] loadSpaces(String filename) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
ArrayList<Space> spaces = new ArrayList<Space>(); // JDK 7 diamond operator
for (String str; (str = reader.readLine()) != null;)
{
// Perform your initialization of the Space object here based on the
// data provided in str, placing them into the spaces ArrayList.
}
[B][U]return spaces.toArray(new Space(str)); [/U][/B]// Used new since it's unknown if there are any elements!
}
This is what I thought of: for each space, it would be like this in the txt file:Boardwalk
300.00 //to be converted
Next Space
Cost of next Space //covert double to fit in parameters for space constructor
-end of board-
#7
Posted 13 May 2011 - 03:24 AM
Bump
#8
Posted 13 May 2011 - 10:36 AM
Format your text field data like this:
Mediterranean Avenue(60) Baltic Avenue(60) etc...You don't need to use float values for your currency, since the Monopoly game does not use any denomination lower than a dollar. Even if it did, you still shouldn't use a float for currency, use an int that represents the smallest divisible unit of currency (in the US that would be pennies). You need to parse this string after you take it in as input, it's not going to be as simple as one line of code, y'know? :)
for (String str; (str = reader.readLine()) != null;)
{
int openParenLoc = str.indexOf('(');
int closeParenLoc = str.indexOf(')');
if (openParenLoc >= closeParenLoc || openParenLoc == -1)
{
try
{
String spaceName = str.substring(0, openParenLoc);
Integer spaceVal = Integer.valueOf(str.substring(openParenLoc + 1, closeParenLoc));
board[++curIndex] = new Space(spaceName, spaceVal.intValue());
}
catch (NumberFormatException e)
{
throw new FileParseException("The value between the parenthesis was not an integer on line " + (curIndex + 1) + ".", e);
// This exception doesn't exist, you have to make your own.
}
}
else
{
throw new FileParseException("The positioning of the parenthesis is incorrect on line" + (curIndex + 1) + ".");
}
}
I didn't test it, but that's the gist of what you're trying to do, and what I meant by "Write your loading code."
Wow I changed my sig!
#9
Posted 13 May 2011 - 02:22 PM
I believe the logic test is a bit off in zeke's post.
if (openParenLoc >= closeParenLoc || openParenLoc == -1)
{
throw new FileParseException("The positioning of the parenthesis is incorrect on line" + (curIndex + 1) + ".");
}
else
{
try
{
String spaceName = str.substring(0, openParenLoc);
Integer spaceVal = Integer.valueOf(str.substring(openParenLoc + 1, closeParenLoc));
board[++curIndex] = new Space(spaceName, spaceVal.intValue());
}
catch (NumberFormatException e)
{
throw new FileParseException("The value between the parenthesis was not an integer on line " + (curIndex + 1) + ".", e);
// This exception doesn't exist, you have to make your own.
}
}
#10
Posted 13 May 2011 - 04:01 PM
@lethalwire: I had changed the if condition from this:
openParenLoc < closeParenLoc || openParenLoc != -1to get rid of the else block, and never changed the structure. Haha, goes to show I really didn't test it. XD
Wow I changed my sig!
#11
Posted 13 May 2011 - 07:14 PM
ZekeDragon said:
@lethalwire: I had changed the if condition from this:
openParenLoc < closeParenLoc || openParenLoc != -1to get rid of the else block, and never changed the structure. Haha, goes to show I really didn't test it. XD
Doh! :)
#12
Posted 14 May 2011 - 12:57 PM
I actually haven't played monopoly in a while and just remembered that the cards for each property have other info defined on them for example:
cost
rent
rent per house
rent per hotel
mortgaged amount
etc.
So can we use the parenthesis substring method to store all that info in different instance variables?
cost
rent
rent per house
rent per hotel
mortgaged amount
etc.
So can we use the parenthesis substring method to store all that info in different instance variables?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









