我正在编写一个使用事件类的程序,其中有一个日历的实例,以及一个字符串类型的描述。 创建一个事件的方法使用一个扫描器来接收月、日、年、小时、分钟和描述。 我遇到的问题是,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());
而这是我得到的输出。
Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012
它跳过了输入描述字符串的空间,结果,描述(最初被设置为一个空白空间--" "),从未被改变。
我怎样才能解决这个问题?
Scanner ss = new Scanner(System.in);
System.out.print("Enter the your Name : ");
// Below Statement used for getting String including sentence
String s = ss.nextLine();
// Below Statement used for return the first word in the sentence
String s = ss.next();