|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Hi, complete Java scrub here. What would be the best way to store something like:
bathroom.add(toothbrush);
in a text file?
It's a very simple game where I need to load the game settings from a text file so maybe in one file the toothbrush is added to another room instead of bathroom.
Basically, if I have something like this in my text file:
bedroom,lamp
How can I have the program perform
bedroom.add(lamp);
when the game starts? Having trouble finding a straightforward solution. Thanks.
|
On December 02 2011 10:25 Mazer wrote:Hi, complete Java scrub here. What would be the best way to store something like: bathroom.add(toothbrush); in a text file? It's a very simple game where I need to load the game settings from a text file so maybe in one file the toothbrush is added to another room instead of bathroom. Basically, if I have something like this in my text file: bedroom,lamp How can I have the program perform bedroom.add(lamp); when the game starts? Having trouble finding a straightforward solution. Thanks.
if(input=="lamp") bedroom.add(lamp);
|
On December 02 2011 10:27 THE_DOMINATOR wrote:Show nested quote +On December 02 2011 10:25 Mazer wrote:Hi, complete Java scrub here. What would be the best way to store something like: bathroom.add(toothbrush); in a text file? It's a very simple game where I need to load the game settings from a text file so maybe in one file the toothbrush is added to another room instead of bathroom. Basically, if I have something like this in my text file: bedroom,lamp How can I have the program perform bedroom.add(lamp); when the game starts? Having trouble finding a straightforward solution. Thanks. if(input=="lamp") bedroom.add(lamp);
But I also need it to recognize the room it's being added to since there are various rooms. The comma is there as a delimiter. I also need it to go line by line.
|
file could be something like this:
bedroom=lamp&bed&computer bathroom=toothbrush&towel
then you need to parse the file with something such as:
while(reader.ready()) { String tokens[] = reader.readLine().split("=");
if(tokens[0].equalsIgnoreCase("bedroom")) { for(String item:tokens[1].split("&")) { bedroom.add(item); } } }
For example, for
bedroom=lamp&bed&computer
after split("=") you would get an array with 2 strings:
[0] => bedroom [1] => lamp&bed&computer
the index 0 of the array would be your room identifier, the index 1 would contain the items in that room. Now you need to parse the itens into the room variable, for that you iterate over the array given by split("&") on the lamp&bed&computer string.
Array resulting from split("&") on lamp&bed&computer
[0] => lamp [1] => bed [2] => computer
|
Hi, thanks for the help so far. It's been great. I'm doing it a little differently but with the same idea.
One more question though, instead of the
if(tokens[0].equalsIgnoreCase("bedroom"))
As you have posted, is there a way to take bedroom straight from the text file to pass to the command or is that tricky since bedroom is a Room object. Just so I can keep myself from adding several if(tokens[0].equalsIgnoreCase("room") statements.
Edit. Hrm, the items are also supposed to be of type Item so I'll need to find a way to add the Item not the String. Kinda similar to the above with a Room not being a string.
|
aers
United States1210 Posts
You can store the rooms in a hashtable which would let you do String lookups to get the appropriate object, and wouldn't require chained if-else statements.
The Items are a little bit trickier since I assume you want a unique object for each item in each room?
|
Edit: Required Silverlight Version isn't publicly available.
Silverlight 5 RC Runtime
Sorry if this is the wrong place or if this isn't allowed. I looked at the rules of this thread and it didn't seem to have anything against it.
I have come to know that this place has some respectable individuals and I've been coming to it for a long time. I know a small amount of people here through starcraft/personally, so I figured this would be the best place.
The hardest part of picking up a new language or new skill set I find is figuring out a dummy project that parallels the process of an actual commercial application in terms of code quality and technological exposure. My solution is to make stupid projects up and convince myself that there's at least a chance they'll become profitable(with an MLP site that large, anything is possible, right?). I'm working on one right now that I've put a couple weeks in (I wrote some custom controls, couldn't find anything better, unfortunately). It's pretty barebones, but it does touch on a lot of new microsoft technologies(Silverlight 5.0, WCF RIA Services, Windows Azure(Microsofts Cloud solution), Entity Framework 4.0, SQL Server 2008 RC2) and uses third party components from Infragistics.
I don't always have a ton of time to work on my personal projects as work is also quite demanding, but if anyone wants to dive into some code that uses those technologies and start playing around with something I'm hoping will get larger, PM me or aim me at logic5467, I'll give a better description of what the project is if anyone is actually interested.
DIYMeme
If you go to the about page, you'll notice that after about 5 years(takes less time when the connection is cached), it'll show some messages boxes filled with information pulled from an Azure database via Entity Framework(wrote as a test to make sure WCF calls were working when deployed), the gallery page has the controls I spent most of the time on. The gallery and "property navigator" filtering thingy were made "in-house"(nothing loads because i recently moved to the cloud which doesn't provide FTP access, unlike my previous hosting that this was working on). The log in is also "working".
|
Hyrule18969 Posts
The version of Silverlight you have requested is not yet publicly available.
Please contact the owner of the Web site that directed you here for further assistance.
sigh
|
On December 06 2011 10:39 tofucake wrote: The version of Silverlight you have requested is not yet publicly available.
Please contact the owner of the Web site that directed you here for further assistance.
sigh
Oh, that's right. It's the RC of Silverlight 5. It should be coming out publicly by year end I believe. Using the developer preview.
Silverlight 5 RC Runtime
|
Hey guys, ive been here before and i need some help again :/. So im having some trouble. I'm writing two classes in Java, one which creates a 2D array of characters which acts as a board or table. The second class represents a turtle that can move about the board. When the turtle moves, it leaves a trail of symbols behind it. The turtle “remembers” what board it is on, its location on the board, its id, the symbols that it draws when it moves, and the direction it is facing. Im not asking you to look through the large block of code fishing for a problem, i think i have the error essentially picked out, i just do not really understand it or how to fix it. public class Turtle {
private int row, col, dir; private char symbol; private static Board tBoard; private static char idCount = '0'; // static variable for ID so it can be remembered private char myId; public Turtle( int newRow, int newCol, char newSymbol, Board newBoard ) { myId = idCount; idCount++; // increment idCount each time, first will be 0 row = newRow; col = newCol; setSymbol( newSymbol ); tBoard = newBoard; tBoard.setRowColumn(row, col, idCount); // prints the initial location of the turtle } // increases or decreases the row or column or both depending on the direction specified public void setDirection( int newDir ) { if( newDir < 0 ) dir = 0; if( newDir > 7 ) dir = 7; else dir = newDir; switch( dir ) { case 0: col++; break; case 1: col++; row--; break; case 2: row--; break; case 3: col--; row--; break; case 4: col--; break; case 5: col--; row++; break; case 6: row++; break; default: col++; row++; } } // mutator method public void setSymbol( char newSymbol ) { symbol = newSymbol; } // accessor method public char getSymbol() { return symbol; } // moves the row and col of the turtle and leaves the trail of symbols public void drawLine( int distance ) { int i; switch( dir ) { // neccessary to set the turtle back to its initial location after calling the setDirection() function case 0: col--; break; case 1: col--; row++; break; case 2: row++; break; case 3: col++; row++; break; case 4: col++; break; case 5: col++; row--; break; case 6: row--; break; default: col--; row--; } for( i = 0; i < distance; i++ ) { tBoard.setRowColumn( row, col, symbol ); // adds trailing symbol to board setDirection( dir ); // moves the turtle tBoard.setRowColumn( row, col, idCount ); // adds id of turtle to board at the new location if( Math.abs(row-tBoard.getRows()) == 1 || Math.abs(col-tBoard.getColumns()) == 1 ) // if the turtle is at the border, break from function break; } } }
This is the class for the "turtle", all seems right to my inexperienced eye, but i keep getting an error. Specifically an indexoutofbound exception dealing with ( i think ) tBoard.setRowColumn( row, col, idCount ); or
public void setRowColumn(int newRow, int newCol, char newChar) { board[newRow][newCol] = newChar; }
The error leads me to believe that somewhere im going past the parameters of my arrays or something similar to that, but i just dont see it :/. Ive been working on this for a while, and i cant stall now lol. Thanks for any help i can get! FYI: if someone decides to help me out, i can provide a client file to test the program Added notes in the code to helpfully clear it up a bit
|
Please at least put code in [ code ] tags (remove spaces) or ideally upload it as a Gist on Github or something Pretty much impossible to read as is.
In any case, the easiest way to solve these sorts of problems is to debug your code and set a breakpoint in setRowColumn. Just look at what its being called with each time until you see the offending out of bound call, then look at the call stack to determine where that call happened from and you should be able to figure it out. Should also be easy from the stack trace given when the exception occurs and your program crashes, but I imagine you've already tried that.
|
Hi guys, new to C here. I am writing a simple program, but I'm having two issues.
1. How do I take a single line of input, say a string of digits, or in my case, a date in the form mm/dd/yyyy, and store the individual integers in an integer array?
2. How do I output all of the elements of an array? If necessary, how do I copy a variable number of elements from an array, starting at a variable element of the array?
The objective of the program is to convert the date format to Month dd, yyyy (for example, December 09, 2011). I have the following array: char *monthnames[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
|
Hi everyone. I'm having some trouble reading raw input data from my keyboard into my program that runs in the console in C (I'm making a crude version of Tetris and need to be able to move left and right without having to hit enter each time). I am trying to read data from /dev/input/event0.
#define INPUTS "/dev/input/event0"
int main() { FILE * input; char data[17]; input = fopen(INPUTS, "r"); if (input != NULL) { printf("input success"); } else { printf("input fail"); } }
When I run the program it says input fail. Do you guys have any suggestions? Thanks so much.
|
On December 09 2011 10:06 Viper_14 wrote:Hi everyone. I'm having some trouble reading raw input data from my keyboard into my program that runs in the console in C (I'm making a crude version of Tetris and need to be able to move left and right without having to hit enter each time). I am trying to read data from /dev/input/event0. #define INPUTS "/dev/input/event0"
int main() { FILE * input; char data[17]; input = fopen(INPUTS, "r"); if (input != NULL) { printf("input success"); } else { printf("input fail"); } }
When I run the program it says input fail. Do you guys have any suggestions? Thanks so much. You probably shouldn't try to write your own input handler. I'm sure there are libraries out there for tracking keyboard events. I know for C++ OpenGL has its own event handlers, so there must be stand-alone handlers out there somewhere.
|
guys I have to write a Java program which can join to gopher servers using sockets and it has to list the directories of the server.
I made a socket which joined to the gopher server, I grabbed the socket's input and outputstreams so the incoming messages come to the console and I also can type commands .
My question is, how should I ask the server to do what I want? Should the server react to the typed in commands I send through the Socket?
at the moment I'm looking up gopher commands (I think that's the key) but I have no idea what I'm doing because I never really did any networking before. If I connect with the socket to somewhere, I'm basically at the basic text level of communication with it, right?
I am reading stuff about tcp/ip and protocols left and right and it gets more and more confusing
|
On December 09 2011 04:16 Demonhunter04 wrote: Hi guys, new to C here. I am writing a simple program, but I'm having two issues.
1. How do I take a single line of input, say a string of digits, or in my case, a date in the form mm/dd/yyyy, and store the individual integers in an integer array?
2. How do I output all of the elements of an array? If necessary, how do I copy a variable number of elements from an array, starting at a variable element of the array?
The objective of the program is to convert the date format to Month dd, yyyy (for example, December 09, 2011). I have the following array: char *monthnames[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
1. here you're trying to parse the string. One way to do this is to first split the string into substrings (or what many people call tokens), and then try to convert each token into an integer. You should practice googling the right library functions to perform these tasks. + Show Spoiler +
2. Use a for loop. for (i = 0; i < arraysize; i++). Use i and your array variable.
|
On December 09 2011 11:52 Gak2 wrote:Show nested quote +On December 09 2011 04:16 Demonhunter04 wrote: Hi guys, new to C here. I am writing a simple program, but I'm having two issues.
1. How do I take a single line of input, say a string of digits, or in my case, a date in the form mm/dd/yyyy, and store the individual integers in an integer array?
2. How do I output all of the elements of an array? If necessary, how do I copy a variable number of elements from an array, starting at a variable element of the array?
The objective of the program is to convert the date format to Month dd, yyyy (for example, December 09, 2011). I have the following array: char *monthnames[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 1. here you're trying to parse the string. One way to do this is to first split the string into substrings (or what many people call tokens), and then try to convert each token into an integer. You should practice googling the right library functions to perform these tasks. + Show Spoiler +2. Use a for loop. for (i = 0; i < arraysize; i++). Use i and your array variable.
Ok, I'll try this stuff....keep in mind that I started learning C last Sunday lol.
|
Well I was able to use strtok and atoi to display the day and year part correctly, but my issue now is using the number input for month to locate a month in the previously posted array, and then displaying it. It currently returns a random 7 digit number.
EDIT: Ok the program works perfectly now, despite giving a couple of warnings on compilation: "assignment makes pointer from integer without a cast". Thanks guys.
|
On December 10 2011 04:29 Demonhunter04 wrote: Well I was able to use strtok and atoi to display the day and year part correctly, but my issue now is using the number input for month to locate a month in the previously posted array, and then displaying it. It currently returns a random 7 digit number.
if you have a month names array, the index of the month -1 will be the index of the correspondent month in that array.
monthnames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; printf("%s", monthnames[5]);
will output
June
because 5 is the index of the string June in your array (remember that indices start at 0).
|
On December 10 2011 04:50 fabiano wrote:Show nested quote +On December 10 2011 04:29 Demonhunter04 wrote: Well I was able to use strtok and atoi to display the day and year part correctly, but my issue now is using the number input for month to locate a month in the previously posted array, and then displaying it. It currently returns a random 7 digit number. if you have a month names array, the index of the month -1 will be the index of the correspondent month in that array. monthnames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; printf("%s", monthnames[7]);
will output June
because 7 is the index of the string June in your array (remember that indices start at 0).
Yeah I understand this but for some reason that method wasn't working and outputted garbage. Instead of 7 I was using an integer variable and it would refuse to compile, saying I could not use a pointer there. It works now, though. I'll include the program below.
#include <stdio.h>
int mm,dd,yyyy; char *mconv=NULL, *dconv=NULL, *yconv=NULL, date[11], delim[]="/", *monthnames[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int main() { printf("Enter the date in the form mm/dd/yyyy.\n"); scanf("%s",&date); mm=atoi(strtok(date,delim)); dconv=strtok(NULL,delim); yconv=strtok(NULL,delim); mm--; dd=atoi(dconv); yyyy=atoi(yconv); printf("You entered the date %s %i, %i",monthnames[mm],dd,yyyy); getch(); return 0; }
|
|
|
|