全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:5631
推到 Plurk!
推到 Facebook!

[問題][JAVA]請問nextLine()跟next()的用法的不同?

答題得分者是:neoart
jeffccl
一般會員


發表:2
回覆:2
積分:0
註冊:2004-12-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-14 14:11:22 IP:154.20.xxx.xxx 未訂閱
小弟寫了一個管理圖書館物品的程式(用array來管理) 裡面會先詢問物品的特性(eg: 書) 以下是我程式裡的一小段
int date;
        String title;
        String author;
        System.out.println("Please enter the title of the book");//1
        title = keyboard.nextLine(); 
        System.out.println("Please enter the author of the book");//2
        author = keyboard.nextLine(); 
        System.out.println("Please enter the year of when this book is published"); //3
        date = keyboard.nextInt();
在程式問第1個問題的時候 我如果打入一個完整的字stephan他就會跳到第二個問題 可是如果我打入有空格的字例如"stephan chou" 程式會讓第2個以及第3個問題同時顯示出來 等於讓我無法key in第二個問題的答案 後來我問助教 助教跟我講說要用keyboard.next()不要用nextLine() 可是當我使用nextLine程式會直接跳過第1個問題卻不忽略2跟3 請問這樣該如何解決呢 這裡有我寫的完整程式(有點長)
import java.io.IOException;
import java.util.Scanner;    /*
 * Created on 2004/12/13
 */    /**
 * @author Jeffrey Chang
 */
public class LibraryApp
{
    public static void main(String[] args) throws IOException
    {
        ItemsArray tracker = new ItemsArray();
        tracker.run();
    }
}    class ItemsArray
{
    private Item[] items;
    private final int MAX_ITEMS = 1000;
    private final static char ADD_BOOK = '1';
    private final static char ADD_CD = '2';
    private final static char ADD_DVD = '3';
    private final static char PRINT_ALL = '4';
    private final static char QUIT = '5';
    private Scanner keyboard;
    private int numItems;
    private int itemsCount = 0;
    
    public ItemsArray()
    {
        keyboard = new Scanner(System.in);
        items = new Item[MAX_ITEMS];
        numItems = 0;
    }
    
    public void run()
    {
        char userOption;
        
        while( true )
        {
            displayMenu();
            userOption = getOption();
            if( userOption == QUIT )
            {
                System.out.println("BYE!");
                break;
            }
            processOption( userOption );          
        }
    }
    
    private void displayMenu()
    {
        System.out.println( "\nPlease choose one of the following options: " );
        System.out.println( "\t"   ADD_BOOK   " Add a new book" );
        System.out.println( "\t"   ADD_CD   " Add a new CD" );
        System.out.println( "\t"   ADD_DVD   " Add a new DVD" );
        System.out.println( "\t"   PRINT_ALL   " Print all information" );
        System.out.println( "\t"   QUIT   " Quit" );
    }
    
    private char getOption() 
    {
        char userOption;
        
        while( true )
        {
            userOption = keyboard.next().charAt( 0 );
            if( isValidOption( userOption ) )
            
                break;
            
            System.out.println( "Choice not valid, try again." );
        }
        return userOption;
    }
    
    private boolean isValidOption( char userOption )
    {
        
        return( userOption >= ADD_BOOK && userOption <= QUIT );
    }
    
    private void processOption( char userOption )
    {
        if( userOption == ADD_BOOK )
        {
            addNewBook();
        }
        else if( userOption == ADD_CD )
        {
            addNewCD();
        }
        else if( userOption == ADD_DVD )
        {
            addNewDVD();
        }
        else if(userOption == PRINT_ALL)
        {
            printItemInfo();
        }
        
    }
    
    private void addNewBook()
    {
        int date;
        String title;
        String author;
        System.out.println("Please enter the title of the book");
        title = keyboard.next();
        System.out.println("Please enter the author of the book");
        author = keyboard.next();
        System.out.println("Please enter the year of when this book is published");
        date = keyboard.nextInt();
        
        if (itemsCount == MAX_ITEMS)
        {
            System.out.println("You can't add any more books!");
        }
        else
            items[itemsCount] = new Book(title,date,author);
        itemsCount  ;
    }        private void addNewCD()
    {
        int date;
        String title;
        String author;
        int length;
        System.out.println("Please enter the title of the CD");
        title = keyboard.next();
        System.out.println("Please enter the composer of the CD");
        author = keyboard.next();
        System.out.println("Please enter the year of when this CD is published");
        date = keyboard.nextInt();
        System.out.println("Please enter the length of this CD");
        length = keyboard.nextInt();
        
        if (itemsCount == MAX_ITEMS)
        {
            System.out.println("You can't add any more CDs!");
        }
        else
            items[itemsCount] = new CD(title,date,author,length);
        itemsCount  ;
    }
    
    private void addNewDVD()
    {
        int date;
        String title;
        String author;
        int length;
        System.out.println("Please enter the title of the DVD");
        title = keyboard.next();
        System.out.println("Please enter the composer of the DVD");
        author = keyboard.next();
        System.out.println("Please enter the year of when this DVD is published");
        date = keyboard.nextInt();
        System.out.println("Please enter the length of this DVD");
        length = keyboard.nextInt();
        
        if (itemsCount == MAX_ITEMS)
        {
            System.out.println("You can't add any more DVDs!");
        }
        else
            items[itemsCount] = new DVD(title,date,author,length);
        itemsCount  ;
    }
    
    private void printItemInfo()
    {
        for(int i = 0; i < itemsCount;i  )
        {
            System.out.println(items[i].toString());
        }
    }
    
}    abstract class Item
{
    protected String title;
    protected int itemNumber;
    protected boolean isBorrowed;
    protected static final int FIRST_NUMBER = 10000;
    protected static int nextNumber = FIRST_NUMBER;
    protected int date;
    protected String author;
    
    
    
    public Item(String title, int date, String author)
    {
        this.title = title;
        this.date = date;
        this.author = author;
        itemNumber = nextNumber;
        nextNumber  ;
        isBorrowed = false;
        
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public int getItemNumber()
    {
        return itemNumber;
    }
    
    public int getDate()
    {
        return date;
    }
    
    public String getAuthor()
    {
        return author;
    }
    
    public boolean borrowItem()
    {
        if(isBorrowed == false)
        {
            isBorrowed = true;
        }
        return isBorrowed;
    }
    
    public boolean returnItem()
    {
        if(isBorrowed == true)
        {
            isBorrowed = false;
        }
        return isBorrowed;
    }
    
    public String getIsBorrowed()
    {
        String result;
        if(isBorrowed == true)
        {
            result = "Yes";
        }
        else
            result = "No";
        
        return result;
    }
    
    abstract public String toString();
}    class DVD extends Item
{
    private int length;        public DVD( String title, int date, String author, int length )
    {
        super( title, date, author );
        this.length = length;        }        public String toString()
    {
        String info;
        info = "\nName of the DVD: "   title   "\nComposer: "   author
                  "\nDate: "   date   "\nLength of DVD: "   length   "mins"
                  "\nBorrowed: "   super.getIsBorrowed();
        return info;
    }    }
class CD extends Item
{        private int length;        public CD( String title, int date, String author, int length )
    {
        super( title, date, author );
        this.length = length;
    }        public String toString()
    {
        String info;
        info = "\nName of the CD: "   title   "\nComposer: "   author
                  "\nDate: "   date   "\nLength of CD: "   length   "mins"
                  "\nBorrowed: "   super.getIsBorrowed();
        return info;
    }    }
class Book extends Item
{               public Book( String title, int date, String author)
    {
        super( title,date,author );        }        public String toString()
    {
        String info;
        info = "\nName of the book: "   title   "\nAuthor: "   author
                  "\nDate: "   date   "\nBorrowed: "   super.getIsBorrowed();
        return info;
    }    }    
發表人 - jeffccl 於 2004/12/14 14:17:31
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-14 14:46:47 IP:61.64.xxx.xxx 未訂閱
用useDelimiter 參考範例: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html (P.S.有考慮用swing做介面嗎?)
jeffccl
一般會員


發表:2
回覆:2
積分:0
註冊:2004-12-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-14 21:22:00 IP:154.20.xxx.xxx 未訂閱
恩...scanner那部份的api...pattern跟matcher的api...都看過了.. 可是還是霧煞煞@@ 主要步驟就是create an instance of Pattern 最後再將title指向那個compile好的p 對吧?
Pattern p = Pattern.compile(title);
title = keyboard.useDelimiter(p);
可是我的eclipse卻說"cannot convert Scanner to String" 還是我根本不需要用到compile指令? 唉...或許我基礎沒有打好吧..這樣就知道為什麼我還沒去碰GUI(雖然稍微看過JFrame等的API..) 不過還是謝謝.....=)
系統時間:2024-05-08 23:22:00
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!