我正在编写一个使用Event类的程序,其中包含一个日历实例和一个String类型的描述.创建事件的方法使用扫描程序来获取月,日,年,小时,分钟和描述.我遇到的问题是Scanner.next()方法只返回空格前的第一个单词.因此,如果输入是"我的生日",则该事件实例的描述仅为"我的".
我做了一些研究,发现人们使用Scanner.nextLine()来解决这个问题,但是当我尝试这个时,它只是跳过输入应该去的地方.以下是我的代码的一部分:
System.out.print("Please enter the event description: ");
String input = scan.nextLine();
e.setDescription(input);
System.out.println("Event description" + e.description);
e.time.set(year, month-1, day, hour, min);
addEvent(e);
System.out.println("Event: "+ e.time.getTime());
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012
Run Code Online (Sandbox Code Playgroud)
它跳过空格以输入描述字符串,因此,描述(最初设置为空格 - "")永远不会更改.
我怎样才能解决这个问题?
因此,我正在一个可以接收多种XML请求的项目中。请求的类型由root标签的第一个子元素确定。因此,您可以获得以下内容:
<a>
<b>
foo
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
要么:
<a>
<c>
foo
</c>
</a>
Run Code Online (Sandbox Code Playgroud)
并foo根据的第一个孩子<a>是<b>还是对进行特定操作<c>。我无法确定确定的第一个孩子的任何简单方法。我想我可以进行重复匹配,首先检查a \ 'b'then a \ 'c'等。但是我需要处理很多不同的请求类型,因此这很快就会变得很笨拙。 Elem.child并没有帮助,它只返回一个元素列表:Element本身。
在不事先知道标签名称的情况下,如何获得Element的第一个孩子?