Having trouble moving on from C++ to java, could anyone help with this small problem. Gotta read inputs of this form (100,2,30) + (1,2,3) Obviously, sometimes there will be more or less than 3 nums, and sometimes there will be zero, ie () Any help would be apreciated - its for an assignment! You can assume input syntax will always be correct if need be. Thanx
I'd try something like this: 1. String.split() to split at the '+' character. Store each string separately (duh :-) ) 2. Use String.split() again, but this time split at the characters '(', ',' ' ' (space) and ')'. This should give you a String array of the individual numbers. You will (I think) get an empty array if there were no numbers (ie '()') 3. Create int arrays of the same lenght as the String arrays you got in step 2. 4. Use Integer.parseInt() to convert strings to integers, and store in the new int arrays. The trickiest thing here is getting the regexps right when using String.split(). I find the biggest challenge in Java is knowing which methods to use, and when...the libraries are massive! Hope that helps.
Joined: Mar 25, 2004
Thanks, but in the assignment im not really allowed to use all those pre-defined methods.. Also, can someone explain the reading in of the string as well. PS I'm up to day 6 of "TEACH YOURSELF jAVA IN 21 DAYS" LoL PPS Please help anyway
Joined: Mar 15, 2004
Well, I'd follow the same steps, but implement the methods myself. Eg, for finding the '+' - iterate through the string until you find a '+' character, suppose it's at index i. Then, the first string you want is the string from 0 to (i-1), and the second is from (i+1) to the (string length - 1). Similarly for other tasks, though it's a lot more work without the library methods! If I were you I'd be finding out exactly which methods I could use, and asking questions about whether you're learning good programming practice or how to do easy things difficult ways because your lecturer is too uncreative to think of something interesting ;-) As for reading a string, if you want to read from standard input (ie from the keyboard when program is run), start with this:
You'll need to catch exceptions of course. And then use reader.readLine(). I hope you can at least use the reader classes ;-) Good luck.
--Tim [ April 01, 2004: Message edited by: Tim West ]