2012-11-21 71 views
2

我很新的編碼,只有有限的知識在VB中。我試圖在java中捕獲這些知識,並試圖創建一個簡單的搜索java程序,該程序根據輸入搜索數組並輸出信息以幫助瞭解循環和多維數組。如果語句和搜索字符串數組使用循環在Java

我不知道爲什麼我的代碼將無法正常工作,

package beers.mdarray; 

import java.util.Scanner; 

public class ProductTest 
{ 
    public static void main(String[] arg) 
    { 
     String [][] beer = { {"TIGER", "2"}, {"BECKS", "2"}, {"STELLA", "3"} }; //creating the 2 by 3 array with names of beer and their corresponding stock levels. 
     System.out.print("What beer do you want to find the stock of?: "); 

     Scanner sc = new Scanner(System.in); 
     String beerQ = sc.next(); // asking the user to input the beer name 

     int tempNum = 1; 
     if (!beerQ.equals(beer[tempNum][1])) 
     { 
      tempNum = tempNum + 1; //locating he location of the beer name in the array using a loop to check each part of the array. 
     } 
     System.out.println(beer[tempNum][2]); //printing the corresponding stock. 
    } 
} 

這是我得到的輸出。然而,我不知道這意味着什麼:

What beer do you want to find the stock of?: BECKS 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 
at beers.mdarray.ProductTest.main(ProductTest.java:20) 

我使用搜索功能找不到太多關於我的問題,即使它看起來像一個簡單的問題。

有可能是一個更簡單的方法來做我想做的事情,我會對此感興趣,但我也想知道爲什麼我的方法不工作。

+1

你可能錯過了數組在Java中是零索引? – Erik

+1

請勿使用String [] [] beer'。用'String name','int quantity'字段讓自己成爲'Beer'類。 – artbristol

回答

5

數組索引從0運行到N - 1,其中N是數組中的元素數。這樣的2索引是一個過去具有2元素的數組的末尾:

System.out.println(beer[tempNum][2]); 
           //^only 0 and 1 are valid. 

注意的tempNum的動初始化開始在所述陣列中的第二元件和啤酒的名稱實際上是在beer[tempNum][0]

有關更多信息,請參閱Java語言規範的Arrays一章。

僅舉擴展的for循環,你可以用它來迭代這個數組:

String [][] beers = { {"TIGER", "2"}, 
         {"BECKS", "2"}, 
         {"STELLA", "3"} }; 

for (String[] beer: beers) 
{ 
    if ("BECKS".equals(beer[0])) 
    { 
     System.out.println(beer[1]); 
     break; 
    } 
} 

替代使用多維數組將使用Map實現之一,這裏的名字啤酒是關鍵,庫存水平是價值:

Map<String, Integer> beers = new HashMap<String, Integer>(); 
beers.put("TIGER", 9); 
beers.put("BECKS", 2); 
beers.put("STELLA", 3); 

Integer stockLevel = beers.get("BECKS"); 
if (stockLevel != null) 
{ 
    System.out.println(stockLevel); 
} 
+0

非常感謝你。這樣愚蠢的錯誤。 在我的代碼中,我改變了 int tempNum = 0; and !beerQ.equals(beer [tempNum] [1]) – SirDodgy

+1

如果這是您正在尋找的答案,那麼接受它:) – Pratik

0

在你問其他問題的解決方案的一部分,你可以試試下面的:

package com.nikoskatsanos.playground; 

import java.util.Scanner; 

public class ProductTest 
{ 
    public static void main(String[] arg) 
    { 
     String [][] beer = { {"TIGER", "2"}, {"BECKS", "2"}, {"STELLA", "3"} }; //creating the 2 by 3 array with names of beer and their corresponding stock levels. 
     System.out.print("What beer do you want to find the stock of?: "); 

     Scanner sc = new Scanner(System.in); 
     String beerQ = sc.next(); // asking the user to input the beer name 

     boolean found = false; 
     int index = 0; 
     while(!found) { 
      if(beer[index][0].equals(beerQ.toUpperCase())) { 
       found = true; 
       continue; 
      } 
      index ++; 
     } 
     System.out.println("The stock of " + beerQ + " is " + beer[index][1]); //printing the corresponding stock. 
    } 
} 

這將是很好的,你可以按照oracle的網站教程。它們非常好,你會很快學到java的基礎知識。

http://docs.oracle.com/javase/tutorial/