get - Extract numbers from a string?! Java -
for program trying figure how can go taking series of number string entered user; extract each number @ spaces , parse them integers.
p.s still newbie programming languages please explain in way can understand, thanks.
this have far:
a user may input series of number this: 15 31 94 87 108 11 7 63 79
public int [] getnumbers (){ scanner reader = new scanner (system.in); string inputstr = reader.nextline(); int[] = new int [10]; string[] numb = new string [10]; int space = inputstr.indexof (" "); int length = inputstr.length(); (int x = 0; x < numb.length; x++){ numb [x] = inputstr.substring (0, space); inputstr = inputstr.substring (space+1); int num = integer.parseint (numb[x]); system.out.println (num); a[x] = num; } return a; } this @ moment first number, shorten string. how go finding next number , ending out having going out of bound ? thank you.
you can using string's split method:
string[] numbers = numb.split(" "); int[] = new int [numbers.length]; (int x = 0; x < numb.length; x++){ a[x] = integer.parseint (numbers[x]);; }
Comments
Post a Comment