开发者

Input data that should not be assumed except that there is one white space

开发者 https://www.devze.com 2022-12-21 02:59 出处:网络
I\'m writing a program that asks the user to enter their birth date. For it, I\'m not suppose to know how the numeric data is to be enter except that there is one white space between the month, day an

I'm writing a program that asks the user to enter their birth date. For it, I'm not suppose to know how the numeric data is to be enter except that there is one white space between the month, day and year. Right now, I have it reading the date as a String on one line and I am unsure how to do it so it meets the spe开发者_运维百科cifications of the program.

Using nextInt() seemed like the way to go but I'm not sure.


If you've got a string containing:

1 1 2001

You might also use the split method, so given your string "readfrominput":

String[] numbers = readfrominput.split(" "); // space is the default delimiter.


String.trim():

public String trim()

Returns a copy of the string, with leading and trailing whitespace omitted.

...

This method may be used to trim whitespace (as defined above) from the beginning and end of a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号