Java扫描器完全绕过字符串输入

Java扫描器完全绕过字符串输入,java,Java,我的部分代码有点问题 if (option1 == 1){ //Add Movie System.out.print('\u000C'); System.out.println(">>Add Movie<<"); System.out.println(""); System.out.print("Title: "); String title = scan.nextLine(); System.out.print("Year: "

我的部分代码有点问题

if (option1 == 1){
   //Add Movie
   System.out.print('\u000C');
   System.out.println(">>Add Movie<<");
   System.out.println("");
   System.out.print("Title: ");
   String title = scan.nextLine();
   System.out.print("Year: ");
   int year = scan.nextInt();
}
完全跳过扫描。nextLine();所以我无法为它输入任何信息

有人知道它为什么这样做吗

编辑:


我已经设法解决了这个问题。如果是
scan.nextLine()错误,未完全移动到新行。谢谢大家:)

也许在这段代码之前有一个
next()
家族

input.nextInt();
input.next();

input.next....(); //Excluding input.nextLine()
它不会消耗额外的换行符,也不会消耗语句所消耗的换行符

String title = scan.nextLine();
要解决此问题,您可以在这些代码之前添加额外的
nextLine()

所以这个代码应该可以工作

scan.nextLine(); //Consume extra new line
System.out.print("Title: ");
String title = scan.nextLine();
System.out.print("Year: ");
int year = scan.nextInt();
泰尔,这是密码

Scanner scan=new Scanner(System.in);
System.out.print("Title: ");
String title = scan.nextLine();

对我来说,代码运行良好-

Scanner scan=new Scanner(System.in);
    System.out.print("title: ");
    String title=scan.nextLine();

    System.out.print("year: ");
    int year=scan.nextInt();
    System.out.println("title is: "+title+" and year is: "+year);
我得到的结果是-

title: king
year: 2015
title is: king and year is: 2015

我怀疑这是你唯一的密码。在执行
System.out.print(“Title:”)之前,您似乎有另一次调用
nextInt
或其他
nextFoo
方法(不包括
nextLine
。你很可能只是赶上了事先没有赶上的回程车。假设我是对的,您可以查看更多信息。请显示您的完整代码。请显示完整代码(以及扫描
选项1
的代码)。最好在删除不必要的代码后粘贴完整的类。@Zaman抱歉,代码扫描是什么意思?@Preet,他指的是为
option1
变量输入的代码。请分享您的完整代码。
title: king
year: 2015
title is: king and year is: 2015