File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Read this: (12,13,14) + (100,2,43,54) into two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read this: (12,13,14) + (100,2,43,54) into two arrays" Watch "Read this: (12,13,14) + (100,2,43,54) into two arrays" New topic

Read this: (12,13,14) + (100,2,43,54) into two arrays

Dan Vujcich

Joined: Mar 25, 2004
Posts: 2
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.
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
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.

Dan Vujcich

Joined: Mar 25, 2004
Posts: 2
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
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
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.

[ April 01, 2004: Message edited by: Tim West ]
I agree. Here's the link:
subject: Read this: (12,13,14) + (100,2,43,54) into two arrays
Similar Threads
Marilyn on vacation?
Can you set the value of a Boolean?
PDF File storing and retrieving
Where in WEB- INF will I put my hibernate DAOs?
ServletContext problem