Java跳过scan.nextline()? [英] Java skips scan.nextline() ?

查看:95
本文介绍了Java跳过scan.nextline()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

Java在LIBRARY MENU后没有读取scan.nextLine()。我不明白为什么会这样。我很感激任何帮助:)

















Hello everyone!
Java does not read the scan.nextLine() after the LIBRARY MENU. I could not understand why this happens. I would appreciate any help :)








package main;
import java.util.*;
import classes.*;
import java.util.Scanner;
public class LibraryTest
{
   public static void main( String[] args)
   {
      //variables
      String input;
      LibraryBook book;
      Library library;
      Scanner scan = new Scanner( System.in);
      LibraryBook b1;    
      LibraryBook b2; 
      LibraryBook b3;
      LibraryBook b4;
      String title;
      int control;
      
      //initializations
      book = null;    
      library = new Library();
      b1 = null;
      b2 = null;
      b3 = null;
      b4 = null;
      title = "";
      control = 0;
      
      library.add( "My Diary 1","Pınar Yücel");
      library.add( "My Diary 2","Pınar Yücel");
      library.add( "My Diary 3","Pınar Yücel");
      library.add( "My Diary 4","Pınar Yücel");
      
      do // do until user enters Exit.
      {
         System.out.println("*************");
         System.out.println("LIBRARY MENU");
         System.out.println("Show");
         System.out.println("Find");
         System.out.println("Add");
         System.out.println("Exit");
         System.out.println("*************");
         
         input = scan.nextLine();
         
         if ( input.equals( "Show"))
         {
            System.out.println( library.toString());
         }
         
         if ( input.equals( "Add"))
         {
            
            System.out.println( "Please enter the title and the author of the book that you want to add");
            library.add( scan.next(), scan.next());
            
         } 
         
         if ( input.equals( "Find"))
         {
            System.out.println( "Please enter the title of the book that you want to find.");
            title = scan.nextLine();
            book = library.findByTitle( title);
            
            if ( book != null)
            {
               System.out.println( "Book selected");
               control = 1;
            }
            else
            {
               System.out.println( "There is no such book!");
            }
            
            
            while ( control == 1) 
            {        
               System.out.println("*************");
               System.out.println("BOOK MENU");
               System.out.println("Loan it");
               System.out.println("Return it");
               System.out.println("Remove it");
               System.out.println("Exit to main menu");
               System.out.println("*************");
               
               input = scan.nextLine();
               
               if ( input.equals( "Loan it"))
               {
                  System.out.println( "Enter the due date");
                  book.loanBook( scan.next());
               }
               
               if ( input.equals( "Exit to main menu"))
               {
                  control = 0;
               }
               
               if ( input.equals( "Remove it"))
               {
                  library.remove ( book);
               }
               
               if ( input.equals( "Return it"))
               {
                  book.returnBook();
               }
               
            }
         }         
      }while ( !input.equals( "Exit"));    
   }   
}





我的尝试:



我用scan.next()



What I have tried:

I tried replacingscan.nextLine() with scan.next()

推荐答案

尝试了replacingscan.nextLine()在下面的代码中......

In the following code ...
if ( input.equals( "Add"))
{

    System.out.println( "Please enter the title and the author of the book that you want to add");
    library.add( scan.next(), scan.next());
    
} 



...你有两次打电话给 scan.next() ,输入接下来的两个单词。然而,这仍然等待消耗线的终点。因此,下次打印菜单时, scan.nextLine()调用将读取该内容,然后再循环循环。


... you have two calls to scan.next(), which inputs the next two words. However that leaves an end of line still waiting to be consumed. So next time the menu is printed, the scan.nextLine() call will read that and then go round the loop again.


这篇关于Java跳过scan.nextline()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆