Java - Reading an Array with Scanner class

Gux

Gux

Associate
Joined
30 Sep 2009
Posts
463
Hey guys, I am stuck on a little problem.

I have a 2-dimensional array that stores strings. I need to use the scanner class and access the second part of the array. For an example, in index 0 I have "Nissan" and "Juke" stored. What i want my program to do, is when the user types in Nissan, I want to get the Juke part printed out to the screen.

I am stuck on how to go about doing this. This is as far as I have gotten.

Using scanner class to fill the array to begin with.
My Array is a string type and is called cars
It holds two string values
String 1 = Cars
String 2 = Model


for (int i = 0; i < cars.length; i++){


if (user.equals(cars))){

System.out.println(model);

This is only part of the code I am stuck on, the rest of the code(filling up the array works fine).

Any help would be greatly appreciated
 
You would be better served using a Hashmap <K,V> (Key : Nissan, Value : Juke).

But this will iterate over your complete array:

Code:
for (int row : arrayName) {
            System.out.println(Arrays.toString(new int[]{row}));
        }

Should be straight forward enough for you to figure out how to out a single index value from Scanner w/ that.
 
Back
Top Bottom