2015-09-30 20 views
3

文件存儲爲:從文件中讀取只有一些字符串,它在棧中的Java

The Lord Of the Rings 
J.R.R. Tolkein 
Great Expectations 
Charles Dickens 
Green Eggs and Ham 
Dr. Seuss 
Tom Sawyer 
Mark Twain 
Moby Dick 
Herman Melville 
The Three Musketeers 
Alexander Dumas 
The Hunger Games 
Suzanne Collins 
1984 
George Orwell 
Gone With the Wind 
Margret Mitchell 
Life of Pi 
Yann Martel 

它有本書第一線的稱號,並在下一行有作者。

我只想從文件中讀取前五本書和作者,然後將其存儲在堆棧中。將前兩本作品添加到堆棧中,然後刪除最後一本。我該怎麼做? 這是我做過什麼

Stack<Book> readingList = new Stack<>(); 



    File myFile = new File("books.txt"); 
    Scanner input = new Scanner(myFile); 
    int i = 0; 
    while (input.hasNext()) { 
     readingList.push(new Book(input.nextLine(), input.nextLine())); 
     System.out.println("Adding: " + readingList.lastElement().getInfo()); 
     readingList.push(new Book(input.nextLine(), input.nextLine())); 
     System.out.println("Adding: " + readingList.lastElement().getInfo()); 
     System.out.println("Reading: " + readingList.pop().getInfo()); 
    } 
+0

的情況下,什麼是你的代碼的問題嗎? – Jens

+0

我不知道如何只讀5本書。這件事是讀整個文件 – Dragon

+3

使用'break'語句,如果我= 5 – Jens

回答

2

Asumming每本書+作者是在文件的一行:

 Stack<Book> readingList = new Stack<>(); 



          File myFile = new File("books.txt"); 
          Scanner input = new Scanner(myFile); 
          int i = 0; 
          int counter = 0; 
          for (int numberOfBookToRead = 0; numberOfBookToRead < 2;numberOfBookToRead++) { 
           try { 
            if(readingList.hasNextLine() && counter <= 4){ // provides that only 4 books are pushed while the iteration is going, and also works 
             readingList.push(new Book(input.nextLine(), input.nextLine())); 
             counter += 1; 
             System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             readingList.push(new Book(input.nextLine(), input.nextLine())); 
             counter +=1; 
             System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             System.out.println("Reading: " + readingList.pop().getInfo()); 
             readingList.pop() = null; 
            }catch(Exception e){ 
              e.printStackTrace(); 
            } 
           } else if(readingList.hasNextLine){ 
              readingList.push(new Book(input.nextLine(), input.nextLine())); 
              System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             } 
          } 

的前五本書使用的for-loop代替while只讀

之後清除堆棧中的最後一項:

readingList.pop() = null; 

因爲我有很多時間。這裏是相同的功能,但用的最大的書可變數目:

Stack<Book> readingList = new Stack<>(); 



          File myFile = new File("books.txt"); 
          Scanner input = new Scanner(myFile); 
          int i = 0; 
          int counter = 0; 
          int maxNumberOfBooks = 10; //could be any number you wish 
          for (int numberOfBookToRead = 0; numberOfBookToRead < Math.round(maxNumberOfBooks/2)-1;numberOfBookToRead++) { 
           try { 
            if(readingList.hasNextLine() && counter <= maxNumberOfBooks-1){ 
             readingList.push(new Book(input.nextLine(), input.nextLine())); 
             counter += 1; 
             System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             readingList.push(new Book(input.nextLine(), input.nextLine())); 
             counter +=1; 
             System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             System.out.println("Reading: " + readingList.pop().getInfo()); 
             readingList.pop() = null; 
            }catch(Exception e){ 
              e.printStackTrace(); 
            } 
           } else if(readingList.hasNextLine){ 
              readingList.push(new Book(input.nextLine(), input.nextLine())); 
              System.out.println("Adding: " + readingList.lastElement().getInfo()); 
             } 
          } 
+0

如果文件只包含4本書,會給出錯誤 – Jens

+0

好吧我編輯它,這很容易。好吧,現在他只讀了一行,如果他有一個下一個:D怎麼樣:P –

+0

WOAH !!!!真棒!謝謝 – Dragon

0

你可能要檢查的異常,在一個錯誤的文件

Stack<Book> readingList = new Stack<>(); 
File myFile = new File("books.txt"); 
Scanner input = new Scanner(myFile); 

for (int count = 0; count < 5; count++) { 

    try { 
     readingList.push(new Book(input.nextLine(), input.nextLine())); 
     System.out.println("Adding: " + readingList.lastElement().getInfo()); 
     System.out.println("Reading: " + readingList.pop().getInfo()); 
    } 
    catch(Exception e) { 

     System.out.println(e.getMessage()); 
    } 
} 
+0

這是一個有用的評論,但不是答案。如果你的文件大於5行,你的代碼將不會執行。在每第二本書之後,該書應該從堆棧中清除 –

相關問題