我正在编写一个使用Event类的程序,其中包含一个日历实例和一个类型为String的描述。创建事件的方法使用Scanner获取月份、日期、年份、小时、分钟和描述。我的问题在于Scanner.next()方法只返回空格之前的第一个单词,因此如果输入是"My Birthday",那么该事件实例的描述只会是"My"。
我做了一些研究,并发现人们用Scanner.nextLine()解决这个问题,但当我尝试时,它跳过了应该输入的位置。下面是我的代码片段:
我做了一些研究,并发现人们用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());
这是我的输出结果:
Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012
它跳过了输入描述字符串的空格,因此描述(最初设置为空格 -“”)从未更改。如何修复?