I'm pretty new to coding and only have limited knowledge in vb. I'm trying to catch that knowledge up in java and am trying to create a simple search java program that searches an array based on an input and outputs information to help learn about loops and multi dimensional arrays.
I'm not sure why my code won't work,
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.
}
}
This is what I get for output however and I'm not sure what it means:
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)
I couldn't find much about my question using the search function even though its seems like a simple problem.
There is probably an easier way to do what I am attempting and I would be interested in that, but I also want to know why my method isnt working.
String [][] beer. Make yourself aBeerclass, withString name,int quantityfields.