would depend.
do you know beforehand how the string object will look like? and with this, I mean: do you know before running the application at which position there will be (an) integer(s)?
otherwise, it might be quite tricky, you'll have to check char by char whether it's a digit, and whether it's part of a larger number or stands on itself.
why would you use a 2d array? I just see data for a single dimension array.
you just use the capabilities of the Integer class to turn every number into an int and store it in the array as you would with every other integer.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
ehm... yes, you can use charAt() (there are other ways too, but ...)
String original = "yep this 123 is a number 456 so was that";
String number = "";
String numbers = "0123456789";
for ( int i = 0; i < original.length(); i++ ){
if ( numbers.contains(""+original.charAt(i))){
while ( numbers.contains(""+original.charAt(i))){
number += original.charAt(i);
i += 1;
}
System.out.println("number = " + number);
number = "";
}
}
this can do part of what you need.. now, you implement this in your code and show how you convert it to an int and store it in an array.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
which is why I said:
this can do part of what you need
the error you describe is what happens if the last character of your inputted String object is a number.
so, if you just want to keep the code 'as is', just add a character of which you know isn't a number at the end of the 'original' String before going through the loop
or, what I would recommend ... investigate it yourself a bit and see how you can improve the code yourself.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433