-2

How can I write a program to input a string (consisting of symbols and numbers) into an array? The program then should store numbers into Array1, and symbols into Array2.

For example I want to input a String like: Im 18 years old & 22 days to an array like [Im 18 years old & 22 days] and then convert it to two arrays:

[Im,years,old,days] and [18,&,22]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user94
  • 1
  • That’s a lot to ask in one Stack Overflow question. Please break your assignment into pieces and ask individual questions about each one. How do I read input in Java? How do I break a string into pieces and store them in an array? How do I check if such a piece is a number or a symbol? How do I create a new array? How do I move pieces from the original array to the new one? – Ole V.V. Jan 16 '19 at 18:25
  • Also please remember to search and research before asking. And to report what your search brought up and how it fell short of solving your problem. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ole V.V. Jan 16 '19 at 18:26

1 Answers1

2

This is an approach, but I'm not sure if this is the best one:

  1. Split String on space (based on your example text) : How to split a String by space
  2. Iterate through the array, returned by the split method. : Iterating through array - java
  3. For each element, determine if its text or just a number. (maybe using regex or worse, using logic that based on catching a numberformatexception) : How do I convert a String to an int in Java?
  4. If string, add to 'Array1', if number, add to 'Array2' : How to add new elements to an array?
mrtmes
  • 21
  • 1