. . . and you can use the String#split method to change that to an array. The link tells you which regular expression you use to match whitespace, but when you quote it as a String literal you will need to double the \ character; not "\???" but "\\???". On each element use the appropriate method of the Integer class to turn it into an int before putting it into the array. Note the array of Strings you get will have a particular length,123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1
Euridice Nobium wrote:doesn't work. no idea why. i barely understand how arrays work.
Euridice Nobium wrote:i feel like i'm in over my head most of the time.
Repeat the procedure, but converting each String to an int and adding 1 before printing it, so you see123
873
8273
12
9438
73
0
843
05
1
93847634
8423
054
67
4
87
26
9
1
Now you can create an int[] array from a keyboard input String. And you have an array exactly the right size, with no blanks anywhere.124
874
8274
13
etc . . .
But some of the text was blue and underlined; it is a link, like this. If you click it, you find something useful (or maybe in some cases, something useless ).Euridice Nobium wrote: . . . i've never seen String#split . . .
Campbell Ritchie wrote:
But some of the text was blue and underlined; it is a link, like this. If you click it, you find something useful (or maybe in some cases, something useless ).Euridice Nobium wrote: . . . i've never seen String#split . . .
From the String#split link quoted earlier. That can be translated into code like thisThe string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Now try with my earlier list of numbers and " " as arguments, but you need quotes around anything with whitespace in. Now you can see how to split a String into its tokens.java StringSplitDemo boo:and:foo : o gives 3 tokens.
Splitting with :
boo
and
foo
Splitting with o gives 3 tokens.
b
:and:f
Please enter your integers, as many as you like, before the enter key:
123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1↩
If you want to see what happens, you will have to try it for yourself!java StringSplitDemo "123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1" \s \\s " "
Splitting with s gives . . .
Splitting with \s gives . . .
Splitting with gives . . .
Please enter your integers, as many as you like, before the enter key:
123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1
Try it for yourself ;-)