• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

--Tim
 
Dan Vujcich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic