除了有一个空格之外不应假设的输入数据

发布于 2024-08-23 05:03:16 字数 158 浏览 12 评论 0原文

我正在编写一个程序,要求用户输入他们的出生日期。对于它,我不知道如何输入数字数据,除了月、日和年之间有一个空格。现在,我让它在一行上将日期作为字符串读取,但我不确定如何做到这一点,以使其符合程序的规范。

使用 nextInt() 似乎是可行的方法,但我不确定。

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 specifications of the program.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-08-30 05:03:16

如果您有一个包含以下内容的字符串:

2001年1月1日

您还可以使用 split 方法,因此给出您的字符串“readfrominput”:

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

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.
辞别 2024-08-30 05:03:16

String.trim()

公共字符串修剪()

返回字符串的副本,省略前导和尾随空格。

...

此方法可用于修剪字符串开头和结尾的空格(如上面所定义)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文