2014-10-18 43 views
0

我一直對這個代碼的項目,我不斷收到錯誤「java.util.InputMismatchException」。我已經搜索並發現了類似的問題,但我沒有得到答案如何適用於我的代碼。我知道我正確地輸入了輸入,所以沒有了。另外,我一直試圖重新格式化我的代碼,但它似乎使情況變得更糟。我很抱歉,如果這顯而易見,我不認識它,我只是開始編碼。謝謝你的耐心。我不斷收到錯誤「java.util.InputMismatchException」在我的代碼

下面是完整的錯誤消息:

Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at songBottlesOfBeer.BottlesOfBeer.main(BottlesOfBeer.java:47) 

這是我的完整代碼段:

package songBottlesOfBeer; 

import java.util.Scanner; 

public class BottlesOfBeer { 

     private static Scanner bottles; 
      public static void number(int n) { 

       if (n>1) { 
        System.out.print(n+" bottles of beer on the wall, "+ 
          n+" bottles of beer, ya' take one down, "+ 
          "ya' pass it around, "); 
        n=n-1 ; 
        System.out.println(n+" bottles of beer on the wall."); 
        number(n); 
       }else{ 

        if(n==1) { 
         System.out.print(n+" bottle of beer on the wall, "+ 
           n+" bottle of beer, ya' take one down, "+ 
           "ya' pass it around, "); 
           n=n-1 ; 
         System.out.println(n +" bottles of beer on the wall."); 
         number(n); 
        }else{ 
         System.out.println("No more bottles of beer on the wall, " + 
           "no bottles of beer, ya' can't take one down, " 
           + "ya' can't pass it around, 'cause there are" 
           + " no more bottles of beer on the wall!"); 
          } 

         } 
       } 

      public static void main(String[] args) { 
       bottles = new Scanner(System.in); 
       bottles.useDelimiter("\n"); 

       System.out.println("Enter the starting number of " 
         + "bottles in the song " 
         + "'99 Bottles of Beer on the Wall':"); 
       number(bottles.nextInt()); 
       } 
} 

誤差位於數(bottles.nextInt());

+0

唯一的例外發生在'BottlesOfBeer.java:47',這是哪一行? – user1803551 2014-10-18 18:57:03

+1

@ user1803551我猜'數字(bottles.nextInt());' – 2014-10-18 18:57:47

+0

@GáborBakos是的,但我們不應該猜測。 OP應該總是知道在代碼中發生錯誤的位置。 – user1803551 2014-10-18 18:58:40

回答

1

卸下線

bottles.useDelimiter("\n"); 

首先使用\n作爲行分離器,因爲它是取決於操作系統所有避免的。如果在正則表達式中使用System.lineSeparator()\R。其次,分隔符是用來標記化(斷開)輸入成幾個部分,這是你需要的不是什麼。按輸入會自動提交一個輸入。

Scanner bottles = new Scanner(System.in); 
    System.out.println(bottles.nextInt()); 
    System.out.println(bottles.nextInt()); 
    System.out.println(bottles.nextInt()); 

第一呼叫將被阻塞的執行等待輸入。輸入由\p{javaWhitespace}+(基本上是空白)的默認分隔符進行標記。讓我們來看看下面的情況:

輸入:1 2 3輸入

輸出:

1 
2 
3 

這是因爲單5字符輸入被符號化分爲3段,這其中,然後通過所謂的順序nextInt方法。

輸入1 2輸入

輸出:

1 
2 
//cursor mark 

這是因爲你「飽和」第2個呼叫nextInt,但第三沒有找到另一個整數,所以它提示用戶輸入(和塊執行)。

輸入:1 2 3 4輸入

輸出:

1 
2 
3 

正如在第一種情況下,只是現在的掃描儀將輸入4和下一個電話將利用它。

記住關閉掃描儀,當你完成

bottles.close(); 
+0

我真的沒有得到我必須改變的東西。因此,自bottle.useDelimiter(「\ n」);是不是我需要做的我把它改爲 bottles.System.lineSeparator(); ? 感謝您告訴我有關掃描儀的關閉,我確實收到了關於此問題的錯誤消息(以前)。 – dragonbunnies 2014-10-18 20:01:12

+0

@dragonbunnies我的答案中的前兩行是你必須改變的。 – user1803551 2014-10-18 20:06:18

1

我tryed你的代碼,它完美的作品..

此異常只是意味着你已經輸入了什麼東西(我不知道是什麼),但它不是一個Integer這就是應該被讀取。

這裏是輸出寫5時,我得到:

在歌曲上牆「進入瓶子的起始數量99瓶啤酒 的:在牆壁上5瓶啤酒,5瓶啤酒,雅' 把一個下來,雅'通過它,四瓶啤酒在牆上。的啤酒4個 瓶在牆上,4瓶啤酒,雅「取一個下來,雅」 周圍通過它,3瓶啤酒在牆壁上。 3瓶啤酒上 牆上,3瓶啤酒,雅「取一個下來,雅」通過它周圍,2 瓶啤酒在牆上。在牆壁上的啤酒2瓶,2瓶啤酒的 ,雅「取一個下來,雅」通過它周圍,啤酒瓶1上 牆上。 1瓶啤酒在牆上,1瓶啤酒,你可以拿下一個 ,你可以繞過它,在牆上有0瓶啤酒。啤酒在牆上沒有更多 瓶,無瓶啤酒,雅「不能把一個 下來,雅」不能通過它身邊,「原因有 啤酒的牆壁上沒有更多的瓶子!

+0

我敢打賭,OP在輸入中輸入一個空格 – drewmoore 2014-10-18 18:58:41

+0

@drewmoore我沒有給它添加空格。 我再次嘗試並得到錯誤消息。我一直在使用這種方法,它顯示了正確的輸入方式嗎? – dragonbunnies 2014-10-18 19:54:39

+1

@dragonbunnies我認爲他使用的是Unix,其中'\ n'是「正確的」行分隔符。如果你在Windows上,並將你的代碼改爲'\ n \ r',它也應該有點工作。 – user1803551 2014-10-18 20:04:40

相關問題