January 25th, 2013, 05:08 PM
Spliting a String and holding variables
Hello guys, I am making a simple Mastermind game, which is going to be played in the console. My task includes starting the game with entering sth like this: "start 4 5 34356". Where start initializes the game, 4 is the number of possible tries to guess, 5 is for the length of the code to be guessed and 34356 is the code itself. The problem is that I have no idea how to write all those things and match every token with its variable. I really need help, because it must be done soon and I am a little bit stuck.
January 26th, 2013, 03:47 AM
Check out the String API and look at the "Split" method.
It's invoked similarly to this:
Where DELIMITER would be a space in your case (Your input is space-delimited).
String sInput = "start 4 5 34356";
String sArr = sInput.split("DELIMITER");
This will create an array of String objects that would look something like this:
[start]   
That should get you started. Please let us know if you have more questions.
<Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
<Tetrad> I don't know whether to be proud or scared to death
Originally Posted by DaWei_M
January 26th, 2013, 06:06 AM
Okay, but then how do I hold the different values in variables ?
January 26th, 2013, 07:17 AM
The split() method puts the separate parts of the String into an array.
January 26th, 2013, 02:59 PM
Look over the reference material (API docs) for Arrays... but for example:
Of course, you'll have to do all the usual safety checks to ensure you have all the required element in the input string, otherwise you could get an Out of Bounds Exception (looking for something which isn't there).
String initialisation = yourArray;
String possibleTries = yourArray;
String codeLength = yourArray;
String code = yourArray;