For my program I am trying to figure how I can go about taking a series of number as string entered by the user; extract each number at spaces and parse them into integers.
P.S I am still a newbie to the programming languages so please explain in a way that I can understand, thanks.
This is what I have so far:
A user may input a series of number like this: 15 31 94 87 108 11 7 63 79
public int [] getNumbers (){
Scanner reader = new Scanner (System.in);
String inputStr = reader.nextLine();
int[] a = new int [10];
String[] numb = new String [10];
int space = inputStr.indexOf (" ");
int length = inputStr.length();
for (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 at the moment get the first number, then shorten the string. How would I go about finding the next number and ending it with out having it going out of bound ? thank you.